c++ - Weird compiler decision -


i've got following code:

enum nums {   };  class cls { public:   cls( nums ); };  void function() {   cls( ); } 

when try compile gcc, following error:

test.cpp: in function ‘void function()’: test.cpp:12:10: error: no matching function call ‘cls::cls()’ test.cpp:12:10: note: candidates are: test.cpp:7:3: note: cls::cls(nums) test.cpp:7:3: note:   candidate expects 1 argument, 0 provided test.cpp:5:7: note: cls::cls(const cls&) test.cpp:5:7: note:   candidate expects 1 argument, 0 provided make: *** [test] error 1 

if replace function this:

void function() {   cls name( ); } 

then works. works if use constructor 2 arguments. not work if add "explicit" constructor.

i gcc somehow parsing defining variable of type "cls" name "a", not familiar such syntax defining variables. eyes, statement defining anonymous temporary variable of type cls, passing "a" parameter.

compiled gcc 4.6.3.

any insights?

thanks, shachar

the parentheses optional. cls (a); same cls a;, declares object a of type cls , default-initializes (which fails because there no matching constructor).

to create temporary value expires @ end of expression, can cls { }; in c++11, or (cls(a)); (or number of more arcane constructs, void(0), cls(a);).

see this answer more ideas.


Comments