News for hackers. Stuff that matters.
Re:Optimizing C++ Const Objects in LLVM by ape4 on Sunday December 27, 2015 @10:42AM (#10797707)
So it seems 'const' has gone the way of 'register' - ignored by the compiler.
Re:Optimizing C++ Const Objects in LLVM by JoeAltmaier on Sunday December 27, 2015 @10:43AM (#10797709)
Just the optimizer. The compiler still enforces it as it reads the code.
Re:Optimizing C++ Const Objects in LLVM by wyldfire on Sunday December 27, 2015 @10:48AM (#10797721)
I'd guess that the optimizer can't really count on it because it could get aliased by code the optimizer can't see and then casted away.
Re:Optimizing C++ Const Objects in LLVM by jjuhl on Sunday December 27, 2015 @11:04AM (#10797780)
Yeah. const_cast (and C-style casts) and mutable existing in the language make const pretty useless to the optimizer. But at least they are trying to get some benefit from it in some cases. A C++ variant (compiler flag) that would remove const_cast, mutable and the cast-const-away parts of C-style casts from the language and allow the optimizer to put more value into 'const' could be interresting.
Re:Optimizing C++ Const Objects in LLVM by pcwalton on Sunday December 27, 2015 @01:04PM (#10798190)
ISO Core C++ is trying to do something like this. They have to, in order to count on the properties of "const" for their static safety analysis.

(I have my reservations about whether ISO Core C++ with the lifetime profile really qualifies as C++ anymore due the massively backwards incompatible nature of the changes, but it'll be interesting nonetheless.)

Re:Optimizing C++ Const Objects in LLVM by twoodfin on Sunday December 27, 2015 @05:37PM (#10799008)
Can you link to more information about this? Googling for "c++ core lifetime profile" just takes me to the Core Guidelines.

Is the idea that they'll codify a bunch of static analysis that disallows problematic constructs & patterns? Could implementers then add something like Rust's ownership checks (assuming full source visibility)?

Re:Optimizing C++ Const Objects in LLVM by steveklabnik on Monday December 28, 2015 @10:10AM (#10801406)
My sibling commentators have given you good links, but it's worth noting that these systems and Rust have a lot of differences. For example, they're explicitly not trying to tackle data races, and the way that they work is different.

I'm excited to see this all play out though!

Re:Optimizing C++ Const Objects in LLVM by gaul on Sunday December 27, 2015 @10:30PM (#10799718)
CppCon 2015: Herb Sutter "Writing Good C++14... By Default":

https://youtu.be/hEx5DNLWGgA

Sutter comments on const_cast around 11m15s.

Re:Optimizing C++ Const Objects in LLVM by pjmlp on Sunday December 27, 2015 @08:33PM (#10799400)
Yes.

Microsoft has delivered a beta version of it with VS 2015 Update 1.

Clang guys are improving clang-tidy for similar errors.

Check Herb Sutter's talk at CppCon 2015.

Re:Optimizing C++ Const Objects in LLVM by plorkyeran on Sunday December 27, 2015 @11:16AM (#10797819)
const_cast<> isn't the root of the problem. If someone passes you a pointer to a const pointer to a non-const thing, they can still just mutate the non-const thing without any casts. You'd have to forbid non-const to const pointer conversions entirely (which would imply removing const_cast<>, as there would no longer be any situations in which it was legal to use).
Re:Optimizing C++ Const Objects in LLVM by jjuhl on Sunday December 27, 2015 @11:21AM (#10797828)
Right. There are lots of corner cases that would need to be identified and considered in the case of a "strong const" C++ variant (like what you point out) - I still think it could be an interresting experiment.
Re:Optimizing C++ Const Objects in LLVM by pwiecz on Sunday December 27, 2015 @01:02PM (#10798183)
What's the problem with the "mutable"? I thought that it does not impair the type safety.
Re:Optimizing C++ Const Objects in LLVM by jjuhl on Monday December 28, 2015 @07:59AM (#10800921)
You can't assume the object is not modified by a const member function if the class has mutable members.
Re:Optimizing C++ Const Objects in LLVM by commentereleven on Sunday December 27, 2015 @11:23AM (#10797834)
Would that mean not being able to use const in unions?
Re:Optimizing C++ Const Objects in LLVM by singiht34-02 on Sunday December 27, 2015 @11:29AM (#10797861)
I've never actually seen a union used in a C++ codebase. What's the usecase? I know people use them in C for bitfields, but outside of embedded (which is almost always just pure C) that just doesn't seem to come up
Re:Optimizing C++ Const Objects in LLVM by JoeAltmaier on Sunday December 27, 2015 @11:33AM (#10797876)
Embedded can be almost anything, C/C++/java and so on. I've seen microcontrollers with 8K of program space, written in C++. Why? Because it optimizes better than the 'C' programmer does.

Unions are used for interpreting any kind of record stream. Reading serialized binary objects from a file? Use a union. Network packets? A union.

Re:Optimizing C++ Const Objects in LLVM by MereInterest on Sunday December 27, 2015 @01:06PM (#10798196)
When reading from a binary source, I've seen that, instead of unions, the char pointer will be cast to a pointer to the appropriate data structure. What is the advantage of using a union instead of casting?
Re:Optimizing C++ Const Objects in LLVM by theseoafs on Sunday December 27, 2015 @01:20PM (#10798234)
Well, for one, unions have defined behavior in C, while passing pointers around and casting them randomly doesn't.

I don't know if this is the best example. Generally you see unions in C to cover "tagged unions", I.e. Those things that would be subsumed by algebraic data types in other languages.

Re:Optimizing C++ Const Objects in LLVM by MereInterest on Sunday December 27, 2015 @07:48PM (#10799293)
Ah, got it. I am used to C++, where accessing the non-active member of the union is undefined behavior. Thank you.
Re:Optimizing C++ Const Objects in LLVM by stephencanon on Sunday December 27, 2015 @07:23PM (#10799239)
The usual way to do this via memcpy, rather than either a union (requires some non-portable extension like "packed") or pointer casting (invokes undefined behavior if alignment isn't satisfied).
Re:Optimizing C++ Const Objects in LLVM by jjuhl on Sunday December 27, 2015 @11:51AM (#10797938)
Here's one use of union in a C++ code base (sf::Event): http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Event...
Re:Optimizing C++ Const Objects in LLVM by berkut on Sunday December 27, 2015 @05:51PM (#10799031)
Tagged pointers - hiding a few bytes in a pointer address.
Re:Optimizing C++ Const Objects in LLVM by tux3 on Sunday December 27, 2015 @12:53PM (#10798149)
Tagged unions are often used as substitute for the missing std::variant
Re:Optimizing C++ Const Objects in LLVM by tbirdz on Sunday December 27, 2015 @08:11PM (#10799349)
Tagged unions are often used in C to provide a form of Sum Type.
Re:Optimizing C++ Const Objects in LLVM by maxlybbert on Sunday December 27, 2015 @06:11PM (#10799076)
Which, to be fair, is all register buys you also: the compiler isn't supposed to allow you to take the address of a register variable (although the Plan 9 compiler didn't enforce the rule).
Re:Optimizing C++ Const Objects in LLVM by jjuhl on Sunday December 27, 2015 @10:59AM (#10797764)
Well, they are actually trying to extract some meaning from it for optimization purposes, so I wouldn't call it 'ignored'.

'inline' would be a better example of another "ignored" keyword.

Re:Optimizing C++ Const Objects in LLVM by ambrop7 on Sunday December 27, 2015 @02:36PM (#10798470)
What useful meaning is there for optimization purpuses?

As far as I know, there is no meaning in pointer-to-const other than it must not be used directly to modify the stored value (if it is you get a compile error). It is always valid from the perspective of language semantics to cast-away the const and modify the pointed-to object, as long as the object is not itself read-only. So, to my knowledge, they could not make any additional assumptions based on pointer-to-const, though they could use it as a hint to look for a possible optimization.

Re:Optimizing C++ Const Objects in LLVM by jjuhl on Monday December 28, 2015 @07:56AM (#10800907)
Did you read the link? It's all explained there.