Observation: the codes pasted below were tested only with GCC 4.4.1, and I'm only interested in them working with GCC.
Hello,
More than once several times I came across an expression about the construction of an object that I did not understand, and only today I noticed what ambiguity is introduced by it. I will explain how to reproduce it and would like to know if there is a way to fix it (C ++ 0x is allowed). Here it is.
Suppose there is a class whose constructor takes only one argument, and this one type of argument is another class with a default constructor. For example:.
struct ArgType {}; class Class { public: Class(ArgType arg); };
If I try to build an object of type Class on the stack, I get ambiguity:
Class c(ArgType()); // is this an object construction or a forward declaration // of a function "c" returning `Class` and taking a pointer // to a function returning `ArgType` and taking no arguments // as argument? (oh yeh, loli haets awkward syntax in teh // saucecode)
I call this the construction of an object, but the compiler insists that it is a declaration inside the function body. For you who still do not understand this, here is a complete working example:
#include <iostream> struct ArgType {}; struct Class {}; ArgType func() { std::cout << "func()\n"; return ArgType(); } int main() { Class c(ArgType()); c(func); // prints "func()\n" } Class c(ArgType funcPtr()) // Class c(ArgType (*funcPtr)()) also works { funcPtr(); return Class(); }
So, enough examples. Can anyone help me get around this without doing anything too anti-idiomatic (I'm a library developer and people love idiomatic libraries)?
- edit
Nothing. This is a hoax. The most unpleasant parsing: why A a (()); work? .
Thank you sbi.
c ++ object-construction forward-declaration ambiguity
Guilherme vieira
source share