开发者

Object construction/Forward function declaration ambiguity

开发者 https://www.devze.com 2022-12-20 03:40 出处:网络
Observation: the codes pasted below were tested only with GCC 4.4.1, and I\'m only interested in them working with GCC.

Observation: the codes pasted below were tested only with GCC 4.4.1, and I'm only interested in them working with GCC.

Hello,

It wasn't for just a few times that I stumbled into an object construction statement that I didn't understand, and it was only today that I noticed what ambiguity was being introduced by it. I'll explain how to reproduce it and would like to know if there's a way to fix it (C++0x allowed). Here it goes.

Suppose there is a class whose constructor takes only one argument, and this one argument's type is another class with a default constructor. E.g.:

struct ArgType {};

class Class
{
public:
    Class(ArgType arg);
};

If I try to construct an object of type Class on the stack, I get an 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 say it's an object construction, but the compiler insists it's a forward declaration inside the function body. For you who still doesn't get it, here is a fully 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 well, enough examples. Anyone can help me get around this without making anyt开发者_如何学Gohing too anti-idiomatic (I'm a library developer, and people like idiomatic libraries)?

-- edit

Never mind. This is a dupe of Most vexing parse: why doesn't A a(()); work?.

Thanks, sbi.


This is known as "C++'s most vexing parse". See here and here.


Let's simplify a little.

int f1();

What's that? The compiler (and I) say it's a forward declaration for a function returning an integer.

How about this?

int f2(double );

The compiler (and I) say it's a forward declaration for a function taking a double argument and returning an int.

So have you tried this:

ClassType c = ClassType(ArgType());

Check out the c++ faq lite on constructors for explanations and examples


Based on the "C++0x allowed", the right answer is (probably) to change the definition to:

Class c(ArgType {});

Simple, straightforward and puts the burden entirely on the user of the library, not the author!

Edit: Yes, the ctor is invoked -- C++ 0x adds List-Initialization as an unambiguous way to delimit initializer lists. It can't be mis-parsed like in your sample, but otherwise the meaning is roughly the same as if you used parentheses. See N3000, the third bullet point under §8.5.4/3. You can write a ctor to receive an initializer list as a single argument, or the items in the initializer list can be matched up with the ctor arguments individually.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号