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?

10

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 c the non-template class case was to be allowed.

15

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật