Is it illegal get `sizeof` non-static member of struct nested within class template?

Viewed 505

In clang/llvm 3.6.2, the following code results in a compilation error when compiling with std=c++11:

template <typename T=void>
class bar
{
public:
    struct foo
    {
        int array[10];
    };

    int baz()
    {
        return sizeof(foo::array);
    }
};

int main(void)
{
    bar<> b;
    return b.baz();
}

Command line invocation:

$ clang++ -std=c++11 nonstatic.cpp -o nonstatic
nonstatic.cpp:12:28: error: invalid use of non-static data member 'array'
        return sizeof(foo::array);
                      ~~~~~^~~~~
nonstatic.cpp:20:14: note: in instantiation of member function
'bar<void>::baz' requested here
    return b.baz();

If I change bar to be no longer a template, as in

class bar
{
public:
    struct foo
    {
        int array[10];
    };

    int baz()
    {
        return sizeof(foo::array);
    }
};

int main(void)
{
    bar b;
    return b.baz();
}

then the code compiles cleanly. Of note, GCC 5.2.1 accepts both versions under std=c++11. Also of note, moving array into the enclosing class template body (but leaving it as a template) also results in clang accepting this.

Which behavior is correct vis-à-vis the standard? Is this a bug in GCC, clang, or both?

(I asked the same question on cfe-users, but so far have received no response).

1 Answers
Related