C++ compilation issue error - C2332: <class: missing tag name>

2.4k views Asked by At

VS 2005 -> Compilation without any errors.

VS 2010 -> Compilation with any errors.

e.g. code snippet

template< bool f > struct static_assert;
template<>         struct static_assert<true> { static_assert() {} };

Compiler Error:

error C2332: 'struct': missing tag name

e.g. code snippet

template< typename T >
class abcd
{
    struct xyz
    {
        xyz();
        char    c;
        T       tt;
    };
public:
    enum { value = sizeof(xyz)-sizeof(T) < sizeof(T) ? sizeof(xyz)-sizeof(T) : sizeof(T) };
};

Compiler Error:

error C2332: 'class': missing tag name

Any suggestions.

1

There are 1 answers

3
Adrian Mole On BEST ANSWER

In your first code snippet, the problem is that static_assert became a reserved keyword in the C++11 Standard (VS-2010 seems to be using this), whereas it was an 'allowed' identifier in previous versions of the language (such as the VS-2005 compiler was using). To resolve this issue, change the name of your structure, even if that means simply changing the case of one or two letters:

template< bool f > struct Static_Assert; // C++ is case-sensitive, so this is OK!
template<>         struct Static_Assert<true> { Static_Assert() { } };

I cannot reproduce the issue in your second code snippet (using VS-2010).