On Tue, 5 Oct 2010, gongweigang@xxxxxxxxx wrote:
Hi, all,
I have following code:
class A { };
class B
{
public:
void foo( A& ra = A() ) { }
};
int main() {
return 0;
}
If I compile with g++, it has following error message: invalid type 'A' for
default argument to 'A&'.
But when I compile it using SunStudio compiler, it works.
With the latest version:
man CC
[...]
-features=a
Enables/disables various C++ language features.
[...]
[no%]rvalueref [Do not] Allow binding a non-const reference to
an rvalue or temporary.
Default: -features=no%rvalueref
The C++ compiler has traditionally been lax in
enforcing the rule that a non-const reference
cannot be bound to a temporary or rvalue. Beginâ
ning with Studio Express 6/10 the C++ compiler
will not allow the invalid code by default. To
restore the old compiler behavior, use the option
-features=rvalueref.
I suspect the error is due to set the default value for reference "ra" with a
constructor.
Can anyone explain whether the above code is valid in C++ standard ? If not,
how to make it work by g++ ?
void foo( A& ra ) { }
void foo( ) { A a; foo(a); }
or:
void foo( A const& ra = A() ) { }
--
Marc Glisse