On 8/5/2013 7:47 AM, Krzysztof Czainski wrote:
2013/8/4 Edward Diener
mailto:eldiener@tropicsoft.com> On 8/4/2013 8:57 AM, gast128 wrote: Returning a 'const X' is meaningless. Just return 'X' instead.
Just to clarify, Edward, I think you're wrong. Consider:
struct X { void go() {} };
X source_a(); X const source_b();
int main() { source_a().go(); // ok source_b().go(); // error
The same goes for passing something as 'const X'.
I assume you mean a function *taking* something as 'const X'. Consider:
void g( X x ); void g( X const x );
The two declarations of g() are equivalent (!), so you could say this 'const' is meaningless ;-). The 'const' here matters in the implementation of function g.
void f( X x, X const y ) { x.go(); // ok y.go(); // error
When you pass or return by value you get a copy of the object being passed so it means nothing to say that the object is 'const'.
Considering what I've written above, I disagree that 'const' means nothing here. When function source_b() returns by const value, it *means* the returned temporary is const. When function f() takes by const value, it *means* the implementation of f() doesn't modify the copy it gets. For the caller of f(), this 'const' is meaningless indeed ;-)
I am aware of the syntax and its meaning. I specified "meaningless" not because the syntax is meaningless ( if it was the compiler would issue an error about the syntax itself ) but because the practical use of specifying 'const' in either situation is meaningless IMO. You are getting a copy of some X. Why would you practically specify that this copied value cannot be changed ? I do not see a practical programming reason for doing anything that way despite your correctly formed examples above. My reply was abrupt in that I should have specified as a suggestion that the 'const' just be dropped to solve the practical problem, not that the problem did not exist.