I recently found the following function definition in some code I was reviewing:
void func( const::std::string& str )
{
// Do something...
}
I am surprised that the const::std::string
appears to be legal ( it compiles with GCC4.4, GCC 4.8, Clang 3.2 & Intel 13.0.1).
Does the standard specify that const
can be used as a namespace
?
The compiler ensures that the constant object is never modified. You can call either constant or non-constant member functions for a non-constant object.
No, const does not help the compiler make faster code. Const is for const-correctness, not optimizations.
const correctness can't improve performance because const_cast and mutable are in the language, and allow code to conformingly break the rules. This gets even worse in C++11, where your const data may e.g. be a pointer to a std::atomic , meaning the compiler has to respect changes made by other threads.
The const keyword allows you to specify whether or not a variable is modifiable. You can use const to prevent modifications to variables and const pointers and const references prevent changing the data pointed to (or referenced).
Does the standard specify that const can be used as a namespace?
No, it does not, because it can't be.
Your code is the same as:
void func( const ::std::string& str );
The first scope resolution operator denotes global namespace.
It is parsed as
const ::std::string& str
where ::std::string
is a valid way to refer to std::string
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With