0

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?

4

2 回答 2

1
template <class T> using item_ptr = std::shared_ptr<T>;

template <class T> class Container
{
    std::vector<item_ptr<T>> list;
};
于 2020-06-29T15:46:22.213 回答
0

如果要在类中使用别名,请在类中定义:

template<class T>
class Container
{    
     using item_ptr = std::shared_ptr<T>;        
     std::vector<item_ptr> list;  
   
};
于 2020-06-29T16:35:57.560 回答