C++ template/aliasing - Type/value mismatch at argument 1 in template parameter list

1.1k Views Asked by At

I'm getting my feet wet with aliasing in templated classes, and haven't been able to compile the following code:

template <class T> using item_ptr = std::shared_ptr<T>;
class Container
{
    std::vector<item_ptr> list;
};

I get two compile errors, a type-value mismatch at argument 1 in template parameter list, and a template argument 2 is invalid. However, if I write the following code instead:

template <class T> //using item_ptr = std::shared_ptr<T>;
class Container
{
    std::vector<std::shared_ptr<T>> list;
};

then it compiles without errors. According to my understanding, these two statements should do the same thing. What am I not understanding correctly?

2

There are 2 best solutions below

0
bipll On
template <class T> using item_ptr = std::shared_ptr<T>;

template <class T> class Container
{
    std::vector<item_ptr<T>> list;
};
0
AR Hovsepyan On

if you want to use aliasing in the class, then define in the class:

template<class T>
class Container
{    
     using item_ptr = std::shared_ptr<T>;        
     std::vector<item_ptr> list;  
   
};