【问题标题】:C++ Using Constructors from CRTP Template-Template Base ClassC++ 使用来自 CRTP 模板模板基类的构造函数
【发布时间】:2020-01-16 02:59:42
【问题描述】:

curiously recursing template pattern (CRTP)模板模板基类继承构造函数的语法是什么?

template<typename T, template<typename> typename U>
struct Base {
    Base(int) { }
};

template<typename T>
struct Derived : public Base<T, Derived> {
    using Base<T, Derived>::Base;
};

int main(int argc, char *argv[]) {
    Derived<double> foo(4);
    return 0;
}

在VS2019中,上述代码导致如下错误:

错误 C3210: 'Base >': 使用声明的成员只能应用于基类成员
错误 C3881:只能从直接基类继承构造函数

使上述代码工作所需的语法是什么?

【问题讨论】:

  • "什么是 [redacted] 需要使上述代码工作?" - C++17 不是 MSVS 的编译器,因为它是 appears
  • @Fureeish 我担心这可能是答案。希望有人能想出一个变通办法。

标签: c++ visual-studio templates using template-templates


【解决方案1】:

这是有效的代码,它是does compile with GCC 9.2。尝试更新版本的编译器或不同的编译器。或与您的实施者联系。

在此之前,does compile with MSVC 现在有一个解决方法:

template<typename T, template<typename> typename U>
struct Base {
    Base(int) { }
};

template<typename T>
struct Derived;

template<typename T>
using Base_type = Base<T, Derived>;

template<typename T>
struct Derived : public Base_type<T> {
    using Base_type<T>::Base;
};

int main() {
    Derived<double> foo(4);
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 2021-04-20
    • 2017-08-27
    • 2011-09-15
    • 2015-12-08
    相关资源
    最近更新 更多