简体   繁体   English

C++ class 模板派生自模板化 class:有没有更好的写法?

[英]C++ class template derives from templated class: is there a better way of writing this?

I wonder if the following code could be written better:我想知道下面的代码是否可以写得更好:

template< typename DataType >
class MyDerivedClass : public MyBaseClass< std::vector< DataType > >
{
public:
    typedef std::vector< DataType> ListDataType;

    // ...
};

Specifically, I would like to eliminate the extra mention of 'std::vector', but this is of course illegal:具体来说,我想删除额外提及的“std::vector”,但这当然是非法的:

template< typename DataType >
class MyDerivedClass : public MyBaseClass< ListDataType >
{
public:
    typedef std::vector< DataType> ListDataType;

    // ...
};

Any ideas?有任何想法吗?

If the goal is just to avoid writing std::vector twice, you can define an additional helper template parameter for it.如果目标只是为了避免编写std::vector两次,您可以为其定义一个额外的辅助模板参数。

template< typename DataType, typename U = std::vector<DataType> >
class MyDerivedClass : public MyBaseClass< U >
{
public:
    using ListDataType = U;
    // ...
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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