简体   繁体   中英

a template argument list is not allowed in a declaration of a primary template

I want to write a template class to calculate the size of tuple,but error occur when compiling.

template <typename... Types>
struct tuple_size_<tuple<Types...>>{
    static constexpr size_t value = sizeof... (Types);
};

enter image description here

This looks like a partial specialization without preceding primary template, which would look like template <typename> struct tuple_size_ {}; .

The problem is that you're providing a partial template specialization but haven't declared the corresponding primary template . That is, there is no class template that you want to specialize in the first place. In other words, it doesn't make sense to specialize something(the class template in your case) that isn't there in the first place.

To solve this you need to provide a declaration for the primary class template:

//primary template 
template<typename> struct tuple_size_; 

//now you can partially specialize the above primary template
struct tuple_size_<tuple<Types...>>{
    static constexpr size_t value = sizeof... (Types);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM