Just learning ML and I was looking for a built-in function similar to Haskell's const. It would be defined something like:
fun const a b = a
Kind of easy to implement myself of course :) but I thought there might be an equivalent standard function that I'm overlooking.
There is no such function in the standard library, so you'll have to define it yourself like you did.
Related
You probably know that datatypes as std::string have some functions in it.
For example std::string.substr().
I want to know, is there a way that i can add more funcionts to those types?
For example:
int a = 42;
a.isLife(); // a function that returns true if a is 42
But isLife() doesn't exist right?
Is there a way i can add it?
No, that's not possible in C++. In C++ you write isLife(a) instead.
There is no way to do so.
Primitive types are primitive, they don't have operator .available. Primitive types are not classes.
You also cannot append anything to a defined class. Once it's defined, it's complete. Otherwise, compiler wouldn't be able to use this class - what if there was one use of a non-changed class, then someone adds something to this class and uses this class. Which version is now used?
In theory, you could edit system headers like <string>, but that's very theoretical. It's against the standard and no compiler supports it AFAIK.
#SonicAsF, you cannot/should not. #MSalters have the correct answer. That is how C++ intends you to extend classes. This is why std::begin, std::end, and std::swap are what they are. This is also why argument-dependent (“Koenig”) lookup works the way it does: if you have a class C in a namespace NS and a function void NS::f(const NS::C&), if you have NS::C c; you can call f(c); unqualified outside NS. Philosophically, C++ considers f to be a part of C’s interface.
This is currently not possible. You either have to write your own type that behaves like and int and you provide a isLife member function or you provide a free function isLife that takes an int
There was a proposal to have a universal call syntax where foo.bar() would call bar(foo) is bar is actually a free fucntion but it was rejected. Something else could come up or you could try proposing your own.
Do note that it is also illegal (per the standard) to add member functions to standard library types. You can add free functions but you are not allowed to modify the implementation.
I'm being curious about if it is possible to override an implemented function. I mean, is there any legal syntax of function declaration / implementation that allows alternative implementation?
Why am I asking? (I know it sounds ridiculus)
First, just of curiosity and expanding my knowledge.
Second, I've learned that the global new can be overrided (Although it is strongly not recommended).
Third, assume that I have written a library: AwsomeLibrary.hpp, which
my friend wants to include.Among a lot of functions, there is a function like void sort(int* arr), which he thinks that he could implement better (and of course call it with the same name).
I mean, is there any legal syntax of function declaration /
implementation that allows alternative implementation?
No. That would break the one-definition rule (ODR).
Second, I've learned that the global new can be overrided (Although
it is strongly not recommended).
Replaceable allocation functions as documented at http://en.cppreference.com/w/cpp/memory/new/operator_new are really just a very special case, a grey area between language and standard library; certainly not something from which you can infer general rules for your own code.
Third, assume that I have written a library: AwsomeLibrary.hpp, which
my friend wants to include. Among a lot of functions, there is a
function like void sort(int* arr), which he thinks that he could
implement better (and of course call it with the same name).
Such problems are beyond the scope of C++. They are more related to source control versioning systems like Git. If, for example, your project is under Git control, then your friend could create a branch of the code with his better implementation.
It is not possible at language level, aside from one "bizarre" language feature you mentioned yourself: replaceable operator new and operator delete functions. These functions can be replaced through a dedicated mechanism, which is why it is formally referred to as replacement (as opposed to overriding or overloading). This feature is not available to the language user for their own functions.
Outside the limits of standard language you can employ such implementation-specific features as weak symbols, which would allow you to create replaceable functions. For example, virtually all functions in GNU standard C library are declared as weak symbols and can be replaced with user-provided implementations.
The latter is exactly what would facilitate replacement of void sort(int* arr) function in your library. However this does not look like a good design for a library. Function replacement capability should probably reserved for debugging/logging and for other internal library-tuning purposes.
In C++17 std::optional is introduced, I was happy about this decision, until I looked at the ref. I know Optional/Maybe from Scala, Haskell and Java 8, where optional is a monad and follows monadic laws. This is not the case in the C++17 implementation. How am I supposed to use std::optional, whithout functions like map and flatMap/bind, whats the advantage using a std::optional vs for example returning -1, or a nullptr from a function if it fails to compute a result?
And more important for me, why wasn't std::optional designed to be a monad, is there a reason?
There is P0798r0 proposal with exactly this, and the associated implementation here on Github. The proposal also refers to general monadic interface proposal, and similarly usable std::expected. Implementations of those are also available.
How am I supposed to use std::optional, whithout functions like map and flatMap/bind
Maybe in Haskell is perfectly usable without fmap, it represents a value that may or may not be there. It also brings to the type system the distinction so you need to handle both cases.
whats the advantage using a std::optional vs for example returning -1, or a nullptr from a function if it fails to compute a result?
How do you know what the error condition is? Is it 0, -1, MAX_INT, nullptr or something else? If I have both a unsigned int and int return value and the int version previously returned -1 should you change them both to MAX_INT or make them return different values? std::optional avoids the problem.
And more important for me, why wasn't std::optional designed to be a monad, is there a reason?
Does C++ have monads at the moment? Until a different abstraction than the container one there isn't really a way to add that functionality.
You can define bind and return over std::optional, so in that sense it is still a Monad.
For instance, a possible bind
template<typename T1, typename T2>
std::optional<T2> bind(std::optional<T1> a, std::function< std::optional<T2>(T1)> f) {
if(a.has_value()) return f(a.value());
return std::optional<T2>{};
}
It is actually probably useful to define this.
As to why the standard library does not ship with this, or something like this, I think the answer is one of preferred style in the language.
Is there any way a C++ beginner can implement something like this? For example:
./timerprogram sortalgorithm.cpp
where timerprogram.cpp at some point does something like argv[1](); to run the function whose name is given by the command-line argument?
Assuming that sortalgorithm.cpp was self-contained and had an array to sort already. I don't need the timing part, just how to call as a function a command-line argument. Is there anything build-in to C++ that will allow me to do this?
No. The answer is no.
Most of the stuff you see about this are inside jokes.
There are silly ways to make it look like its working, but they are silly, and certainly not for beginners.
Function names are used mostly by the compiler, to compile the code, and figure out when something calls a function "where" it actually is. Also by the linker too, but that's beside the point.
Although some C++ implementations might provide run-time extensions or libraries that can be used to resolve an address given its symbol name, the easiest and the most portable solution is for your program to simply have an array of strings, with your function names, and a pointer to the corresponding function.
Then, your main() searches the array for the requested function name, and invokes it via its function pointer.
How to implement this simple solution is going to be your homework assignment.
This question is related to: Check if a class has a member function of a given signature
Is this functionality implemented by C++11 standard or do I need to use custom implementation?
Is this functionality implemented by C++11 standard
No, it's not. Implementing something like this generally had to be done using macros, to pass the function name in.
do I need to use custom implementation?
Probably not. Such stuff is seldomly needed. There might be other solutions to your underlying problem.