c++templates

Why is inline variable declaration not allowed for template structs in C++?


The wording of the title is a bit iffy but I couldn't come up with a better one. I hope to explain what I am talking about with these examples:

struct S
{ } s;

is a somewhat common pattern in C++. It declares a struct S and an instance s thereof.

Why is

template<typename T>
struct S
{ } s;

not allowed?

I had expected this to declare a template class template<typename T> struct S and a variable template template<typename T> S<T> s. Instead it results in a compilation error.

The conventional way of doing this would of course be

template<typename T>
struct S { };
template<typename T>
S<T> s;

but if the simplified syntax works for normal classes why not also for template classes?

If the proposed syntax were allowed one could also do this

template<typename T>
struct
{ } s;

to declare a variable template of an unnamed template class. AFAIK this pattern can't be emulated with the traditional approach of two independant declarations.

Admittedly, this is a very niche situation but to me it seems inconsistent to allow this syntax for conventional class declarations but not for class template declarations.

Is this an oversight or is there an actual reason for this syntax to be forbidden?


Solution

  • Why is template<typename T> struct S { } s; not allowed?

    Language-lawyer Explanation

    This is explicitly disallowed as per temp.pre:

    In a template-declaration, explicit specialization, or explicit instantiation, the init-declarator-list in the declaration shall contain at most one declarator. When such a declaration is used to declare a class template, no declarator is permitted.

    (emphasis mine)

    Also related is CWG2862


    Possible Rationale for disallowing `s` to be a variable template

    While declaring a variable we need to specify a type but since S is a class template(not a type) and moreover s cannot make use of injected class name outside the class so the type of s cannot be interpreted as S<T>, so this is not allowed.

    Note that one rationale for not considering s to be a variable template may be because a class template and a variable template are two different entities. When using the keyword struct or class it seems more intentional that the user wants a class template than a variable template. For example, consider the declaration

    template<typename T> struct S
    { 
        struct nested
        {
            int x;
        };
    } s;
    

    which looks more like a class template declaration than a variable template declaration imo. So having separate syntaxes for these two different things makes sense. If you want to declare a class template then just use struct or class and if you want to declare a variable template then don't use those two keywords. Mixing and allowing theses two things will be very confusing specially in a language like c++ where there are already too many complications.

    Note that this is not a problem with an ordinary non-template class-type because it is a type and we don't need injected class name in that ordinary case and at the time due backward compatibility with the non-template class case was to be allowed.