News for hackers. Stuff that matters.
Re:Is the C++ preprocessor still needed? by tonmoy on Tuesday May 09, 2017 @11:46PM (#14305786)
Tried 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}’

Re:Is the C++ preprocessor still needed? by Manishearth on Wednesday May 10, 2017 @02:03AM (#14306206)
Heh, 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.
Re:Is the C++ preprocessor still needed? by Negitivefrags on Wednesday May 10, 2017 @02:26AM (#14306287)
I 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.

Re:Is the C++ preprocessor still needed? by Manishearth on Wednesday May 10, 2017 @08:18PM (#14312922)
I mean, this isn't the only case of broken parametricity I've come across in C++ codebases. Though it's the only one in the stdlib.

I know vector<bool> is controversial (and I understand it can't be "fixed" now), I was just surprised that something breaking parametricity so blatantly was ever agreed upon in the first place -- in more functional languages parametricity is kinda much more jealously guarded. Bit of a culture shock there, is all :)

Re:Is the C++ preprocessor still needed? by marvy on Wednesday May 10, 2017 @11:59AM (#14309219)
You mean 8x more memory, right?
Re:Is the C++ preprocessor still needed? by tonmoy on Tuesday May 09, 2017 @11:49PM (#14305794)
works great on vector<int> though, it turns out vector<bool> is a special case!

http://stackoverflow.com/questions/30376032/error-invalid-in...

Re:Is the C++ preprocessor still needed? by klipt on Wednesday May 10, 2017 @12:50AM (#14305975)
Does const auto& work though?
Re:Is the C++ preprocessor still needed? by gpderetta on Wednesday May 10, 2017 @04:29AM (#14306729)
I normally use for(auto&& x : ...) which works with those rare containers that have proxy references, while still allowing mutation.