Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 ambiguity between single qualified name and two consecutive?

Tags:

c++

c++11

Is the following C++11 program ill-formed?

struct a
{
    struct b {  };

    void f() {};
};

extern struct a b;

struct a ::b;

int main()
{
    b.f();
}

Why / why not?

The thing of interest here is this line:

struct a ::b;

Is this a forward declaration of the inner class a::b?

Or is this a definition of the global variable b? Equivalent to:

struct a (::b);
like image 493
Andrew Tomazos Avatar asked Jul 30 '13 21:07

Andrew Tomazos


1 Answers

struct a ::b; doesn't declare a variable named b of type a, if that's what you are asking. It's a (redundant) forward declaration of the nested type a::b. Whitespace is not generally significant in a C++ program. So your program declares, but never defines, a variable named b. That's a violation of One Definition Rule: the program is therefore ill-formed, and the linker will tell you as much.

like image 156
Igor Tandetnik Avatar answered Sep 24 '22 16:09

Igor Tandetnik