c++ - 如何子类化由特征选择的父类(super class)?

标签 c++ templates inheritance

我有一个类(我们称它为 A)的模板,它应该根据我的自定义特征结构中的编译时条件对其他类(B 或 C)中的一个进行子类化。我附上了一个重现该行为的片段。

#include <type_traits>

template<typename T>
class cls_template {
public:
  using method_arg_type = T;
  virtual void method(method_arg_type) = 0;
};

using A = cls_template<float>;
using B = cls_template<int>;

template<typename T>
struct traits {
  using cls = std::conditional<std::is_floating_point<T>::value, A, B>;
};

//class C : public traits<float>::cls {
class C : public A {
public:
  virtual void method(method_arg_type arg) {};
};

int main() {
  A *a = new C();
}

如果我这样保留它(将 A 硬编码为父类(super class)),一切正常。但是,一旦我用注释掉的行替换类定义,我就会收到以下错误消息:

test.cpp:21:27: error: unknown type name 'method_arg_type'
  virtual void method(method_arg_type arg) {};
                      ^
test.cpp:25:10: error: cannot initialize a variable of type 'A *'
  (aka 'cls_template<float> *') with an rvalue of type 'C *'
  A *a = new C();
     ^   ~~~~~~~

为什么不再定义method_arg_type?为什么 C 不再被识别为 A 的子类?我发现如果 traits 不是模板(如果我只是将一个类型硬编码到结构中),它一切正常。

最佳答案

您正在尝试从 traits<float>::cls 派生这是类型 std::conditional<std::is_floating_point<T>::value, A, B> .既不是 A也不B , 它是 conditional 的特化模板。要么来自 ::type的,它将按您的预期工作,或使用 conditional_t (C++14)。

class C : public traits<float>::cls::type { // ok
                                   ^^^^^^
public:
  virtual void method(method_arg_type arg) {};
};

demo

template<typename T>
struct traits {
  using cls = std::conditional_t<std::is_floating_point<T>::value, A, B>;
                              ^^
  // above is C++14, in C++11 you'd have to write
  // using cls = typename std::conditional<std::is_floating_point<T>::value, A, B>::type;
};

class C : public traits<float>::cls { // also ok
public:
  virtual void method(method_arg_type arg) {};
};

demo

关于c++ - 如何子类化由特征选择的父类(super class)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40535707/

相关文章:

c++:类外的模板变量

c++ - boost::this_thread::get_id 在 exit() 调用期间不返回 id

c++ - std::tuple_cat 但只有独特的元素

c++ - 如何创建用于加密的自定义 ofstream 类

c++ - 我的 makefile 不断地自行编译;我究竟做错了什么?

c++ - CMake add_custom_command 没有输出

c++ - 递归构建可变参数函数的返回类型时的奇怪行为

c++ - 在模板函数返回类型上使用 std::enable_if 来利用 SFINAE - 编译错误

python - 子类化: Is it possible to override a property with a conventional attribute?

表示单继承的 C++ 数据结构