简体   繁体   中英

template parameters default value

I'm trying my very first template. the following code compiles :

template<class T,class C=int> class MyClass
{};

But not this :

#include <vector>

using namespace std;
template<class T,class C=vector<T>> class MyClass
{};

Yet i see the standard vector class template declared like this :

template < class T, class Alloc=allocator<T> > class vector
{};

The errors the compiler throws are :

*error: spurious '>>', use '>' to terminate a template argument list
*error: definition of 'class MyClass' inside template parameter list
*error: two or more data types in declaration of 'type name'
*error: expected '>' before ';' token
*error: expected unqualified-id before ';' token

http://coliru.stacked-crooked.com/a/f93734d989e10446

No, it works. You just forgot the std namespace.

the code was :

#include <vector>

using namespace std;
template<class T,class C=vector<T>> class MyClass
{};

but i should have written :

#include <vector>

template<class T,class C=std::vector<T> > class MyClass//with space between right angle brackets  : '> >'
{};

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