Is this legal c++0x syntax?
class A
{
public:
void some_function( const std::set<std::string> &options = {} );
// note that this is legal, which binds the const reference to a temporary:
void some_function( const std::set<std::string> &options = std::set<std::string>() );
}
Because if so, I just found a bug in GCC 4.6.
The error I get is:
error: expected primary-expression before '{' token
which is ... logical ... if it was illegal.
UPDATE: As @Kerrek has illustrated, this bleeds into plain C++03, with aggregates and the old brace initialization syntax for them. Why is this not possible? Is it forbidden in the Standard? Or are compilers at fault? Or is this an oversight? I don't see any serious problems in allowing this as an alternative to explicitely calling the constructor.
Uniform initialization is a feature in C++ 11 that allows the usage of a consistent syntax to initialize variables and objects ranging from primitive type to aggregates. In other words, it introduces brace-initialization that uses braces ({}) to enclose initializer values.
A default argument is a value in the function declaration automatically assigned by the compiler if the calling function does not pass any value to that argument. The values passed in the default arguments are not constant. These values can be overwritten if the value is passed to the function.
Default constructors are one of the special member functions. If no constructors are declared in a class, the compiler provides an implicit inline default constructor. If you rely on an implicit default constructor, be sure to initialize members in the class definition, as shown in the previous example.
The compiler-defined default constructor is required to do certain initialization of class internals. It will not touch the data members or plain old data types (aggregates like an array, structures, etc…). However, the compiler generates code for the default constructor based on the situation.
It is valid in C++11, but it was a very late addition to the working paper that Bjarne put through. So it's not surprising that GCC doesn't support brace default arguments yet.
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