8 ms·
I think that now having auto, decltype, and decltype(auto) all in the language is confusing at best. constexpr also has slightly unintuitive semantics. The prob
by s_ngularity 9y ago
I think that now having auto, decltype, and decltype(auto) all in the language is confusing at best. constexpr also has slightly unintuitive semantics. The problem with all of the additions to C++ isn't whether they're useful individually, it's how as a whole the language is basically a gigantic ball of special cases. C++ is extremely powerful, but also nearly impossible to understand fully.
- tonmoy 9y agoI used to write for(map<int, vector<vector<int> > >::iterator it=m.being(); it != m.end(); it++) for(vector<vector<int> >::iterator it2 = it->second.begin(); it2 != it->second.end(); it2++) Now I have to write for (auto paths : m) { for (auto path : paths) { Sure clang annoys me with not being able to compile with -g, but still auto is a lifesaver!
- Negitivefrags 9y agoThis is neither here nor there, but in your revised version you are copying all the vectors many times. Use auto&.
- jjawssd 9y agoWhile perfectly fine, this comment unironically reinforces the GP's assertion of "how [C++] as a whole the language is basically a gigantic ball of special cases"
- kobeya 9y agoWhat specifically annoys me about C++ and is demonstrated by this example is that the simplest possible expression of an algorithm is almost certainly not the most efficient or desirable way to do what you are trying to accomplish. In a language designed for humans, the simplest expression of a loop (e.g. `for (auto paths : m) {}`) would use references not value copying. You'd have to go out of your way to do the it the dumb way, which you would only do when you have a specific reason to do so. C++, on the other hand, is only really an effective language in the hands of people who have either fully internalized the 1,000 page specification, or wasted a depressing number of synaptic connections on memorizing minute trivia about what constructs to use under what circumstances. I say this as someone who works with C++ on a daily basis :(
- llukas 9y ago> C++, on the other hand, is only really an effective language in the hands of people who have either fully internalized the 1,000 page specification, or wasted a depressing number of synaptic connections on memorizing minute trivia about what constructs to use under what circumstances. No. You use profiling tools and optimize hotspots. Pareto principle applies here so you look at 20% of program tops. Rest is irrelevant and can be executed as inefficiently as you please as long as you got your big O complexity right.
- kobeya 9y agoOr the language and compiler can just save you the trouble and get it right from the beginning.
- tonmoy 9y agoTried this on a vector<bool>, got this error: error: invalid initialization of non-const reference of type ‘std::_Bit_reference&’ from an rvalue of type ‘std::_Bit_iterator::reference {aka std::_Bit_reference}’
- tonmoy 9y agoworks great on vector<int> though, it turns out vector<bool> is a special case! http://stackoverflow.com/questions/30376032/error-invalid-initialization-of-non-const-reference-of-type-bool-from-an-rva http://stackoverflow.com/questions/30376032/error-invalid-in...
- klipt 9y agoDoes const auto& work though?
- gpderetta 9y agoI normally use for(auto&& x : ...) which works with those rare containers that have proxy references, while still allowing mutation.
- Manishearth 9y agoHeh, when I first heard that C++ vectors were bitvectors I was like "that can't be right? You can't get sensible references to members that way!" But it turns out that C++ is fine with breaking parametricity so references to bitvector elements just don't compile.
- Negitivefrags 9y agoI wouldn't say C++ is fine with it. vector<bool> is widely viewed as a mistake. I would actually say it's one of the few inconsistencies in a standard library that tends to be very consistent. The problem is that (quite rightly) the C++ standard committee tries very hard to not break existing code. There isn't really an easy way to deprecate a specialization of this container without causing 32x more memory use if someone was relying on it, and even worse it would be a silent breaking change.
- burstmode 9y agoYep, the "auto" keyword was added because even the inventors of the template system had a understanding of the syntax they created.