简体   繁体   中英

C++ class template with a non templated class?

Compiling the following code

template<typename T>
class t1 {
};

class t1 {
};

Gives the following error

error: template argument required for ‘class t1’

It's literally been years since I've used C++ enough to delve into templates (I've decided to pick it back up and learn C++ 11/14), so I'm probably mistaken, but I thought this was legal.

Can someone advise? If you wanted to do something like this, how would you go about doing it?

No, you can't do this.

Unlike a function template, a class template cannot be overloaded. t1 has been declared as a class template, the only thing you can do is specialize it:

template <>
class t1<int> {
};

You could do something like this with variadic templates:

template<typename...>
struct myclass;

template<>
struct myclass<>{};

template<typename T>
struct myclass<T>{};

But I can think of little use other than template metaprogramming and you still have to use template syntax: myclass<>

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