根据基本类型

问题描述 投票:0回答:1
为了解决方法,我可以编写通过STD ::选择条件的新的基类包装器,但肯定不是很好:

template<class T> struct CVector : public std::vector<T> { using std::vector<T>::vector; protected: using std::vector<T>::assign; using std::vector<T>::emplace_back; using std::vector<T>::insert; using std::vector<T>::push_back; using std::vector<T>::resize; using std::vector<T>::swap; }; template <class T> struct CList : public std::list<T> { using std::list<T>::list; protected: using std::list<T>::assign; using std::list<T>::emplace_back; using std::list<T>::insert; using std::list<T>::push_back; using std::list<T>::resize; using std::list<T>::swap; using std::list<T>::emplace_front; using std::list<T>::merge; using std::list<T>::push_front; using std::list<T>::splice; }; template <class ValueType, class BaseType = std::vector<ValueType>, std::enable_if_t<std::is_same_v<BaseType, std::vector<ValueType>> || std::is_same_v<BaseType, std::list<ValueType>>,int> = 0 > struct Test : public std::conditional<std::is_same_v<BaseType, std::vector<ValueType>>, CVector<ValueType>, CList<ValueType>>::type { Test() = default; }
    

标准C ++中最强大和可维护的方法确实是您描述的解决方法。为什么?因为使用声明为
不模板
inheritance c++17 type-traits
1个回答
0
投票
,即您不能直接将

std::enable_if直接应用于使用声明以有条件地包括它。一旦实例化,使用使用声明的存在将固定。 通过将使用声明分配到CVECTOR和CLIST中,您可以将隐藏在其自身上下文中的容器特定方法隔离。通过STD ::有条件选择正确的基础,以确保存在使用声明的有效基础。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.