726 karma · joined January 27, 2016
In it's terminology, std::variant is a 'discriminated union' instead of a 'sum type'. Making std::variant the latter would also have had disadvantages it seems.
That's exactly what a tagged union does. std::variant is a tagged union. I was not aware of the name 'sum type' but it's supposed to be a synonim for a tagged union. Guess not, but std::variant is not meant as what you describe[1].
[1]: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n454...
See my edit.
std::visit([](auto &&val) {
std::cout << typeIdxToString[typeid(val)] << val;
}, var);
> E.g. you would want the '+' operator to do different things for strings and numbers.Then overload the '+' operator, no need to put all that in the visitor lambda.
Then the answer is yes. We haven't advanced to the point where the compiler can guess that you want. If you can give me an actual example of when it would be an insurmountable task to do so, please let me know.
std::variant<size_t, std::string> var = "test";
std::visit([](auto &&val) { std::cout << val; }, var);
If you also want to print the name of the type of the variable, along with the value itself, well, C++ doesn't have reflection (yet). So you're going to have to write a function that takes a value and returns its type as a string, which you can already do using the typeid operator. This is basically implementing reflection yourself, it is cumberstone but doesn't require advanced template programming.There no need for any of the madness with explicit types inside the visitor lambda, which is deemed as neccesary by the author.
Edit: here's another way to map types to strings. So no templates necessary at all for this entire problem.
std::map<std::type_index, std::string> typeIdxToString;
typeIdxToString[typeid(std::string)] = "string";
typeIdxToString[typeid(int)] = "int";
// ...
Edit2: Turns out C++ has some form of reflection after all. You can just use: typeid(val).name()
If your compiler supports pretty names.2. see 1
3. not sure what you mean by this
We've had it since C++11. http://en.cppreference.com/w/cpp/language/storage_duration
https://championtraveler.com/dates/best-time-to-visit-cuba/
But really great idea!
What I meant was that IRC is still alive and kicking.
Seriously?
namespace redis = cpp_redis;
there you go.This is not good UX http://i.imgur.com/en3lBF5.jpg
They mention the FCC as if non-US citizens are supposed to know what that is. They ask for a zip code, without asking for a country. The phone number they listed will not work anywhere but in the US. The address listed in their privacy policy does not name a country.
All this leads me to believe that the people behind this think the Internet only exists in the US, or that they are not even aware there are other countries besides the US. Don't worry, you're not the one that's stupid here.