News for hackers. Stuff that matters.
Re:Hello. I'm a compiler by keeperofdakeys on Tuesday February 19, 2013 @07:38AM (#5243937)
There are some magic keywords, like const, which definitely help the compiler. Unfortunately these kinds of optimisations are far too numerous, and soon become tedious. After a certain point, more structured languages could allow much more rigid optimisation. These days JIT seems to have taken over though, which isn't really a bad thing.
Re:Hello. I'm a compiler by AshleysBrain on Tuesday February 19, 2013 @09:48AM (#5244517)
Const doesn't actually help compilers (at least in C/C++ based languages). Since it can be const_cast away and still work in a standards compliant way, any optimisation assuming const values never change is a bug. Instead the compiler will use other techniques, e.g. finding variables that are only written to on initialisation, and those don't even need to be marked const.
Re:Hello. I'm a compiler by koenigdavidmj on Tuesday February 19, 2013 @01:47PM (#5246201)
I've seen gcc turn a const bool& into a direct pass of a bool by value. Of course, I noticed this because it was the implementation of a web service binding, and the wrapper was going to pass me a bool& (as a pointer) whether I liked it or not. Instant segfault...but not always :(
Re:Hello. I'm a compiler by _kst_ on Tuesday February 19, 2013 @12:17PM (#5245520)
If an object is defined as const, casting away its constness and modifying the object causes the program's behavior to be undefined. Compilers are free to take advantage of that.
Re:Hello. I'm a compiler by AshleysBrain on Tuesday February 19, 2013 @12:33PM (#5245648)
Do you have a spec reference for that? I was under the impression you could safely const_cast and modify pretty much anything. Perhaps it was just primitives like ints, or const member functions.
Re:Hello. I'm a compiler by mikeash on Tuesday February 19, 2013 @12:36PM (#5245679)
That is true, but it's extremely rare to be able to take advantage of that. When people think of const as a potential optimization aid, it's almost always in the context of const pointers, which actually do nothing at all for optimization.
Re:Hello. I'm a compiler by bitwize on Tuesday February 19, 2013 @12:07PM (#5245448)
Oh God. Don't get me started on const.

    const char * const fuck_this(const char *shit) const;
Thing #1: In C++ the word "const" can mean many things depending on position and inflection: declaring a symbol an actual constant; a pointer argument has in (as opposed to in-out) semantics; a function has no side effects. I keep forgetting which use of const does what.

Thing #2: Each use of const, regardless of meaning, induces a different type. Which makes API design a pain in the ass since you can't pass in stuff that wasn't declared with the same constness if the procedure expects const parameters without casting the parameters. I actually worked on a codebase where a guy broke much of our code by checking in a "fix" for our APIs to make them comply with what he considered best practice regarding constness.

Thing #3: Passed-in pointers and references are not 'const' by default. You should have to declare intent to clobber their referents.

Thing #4: And as someone said downthread, you can cast around it in most cases anyways.

This is why I say, if you're thinking of using C++, reach for Ada instead. Ada's reputation has been necessarily tainted by whiners complaining about its verbosity. But I swear that figuring out a page of Ada code is easier than figuring out a few lines of sufficiently complicated C++. For one thing, in Ada, function/procedure parameters are 'in' (i.e., const) by default, and you must declare intent to modify parameters in side effects by declaring them 'in out'.