小编Joh*_*yne的帖子

使用enable_if禁用模板类的模板构造函数

我正在尝试使用模板构造函数std::enable_if的参数类型匹配类型" MyClass" 时禁用模板类的模板构造函数,以便我可以使用我的其他构造函数,这允许我使用类初始化当前模板的类另一个

template <typename t, size_t size>
class MyClass
{
public: 
   MyClass() { data.fill(static_cast<T>(0)); }

   template <typename... Args> // i want to disable this if Args = MyClass
   MyClass(Args&&... args) : data{ std::forward<Args>(args)... } {}

   template <size_t size2>
   MyClass(const Myclass<t, size2>& other_sized_template) { /*do stuff*/ } // this won't work unless the template ctor above is disabled if the arguments passed are of type Myclass

private:
   std::array<t, size> data;
};
Run Code Online (Sandbox Code Playgroud)

c++ templates sfinae variadic-templates c++11

5
推荐指数
1
解决办法
803
查看次数

标签 统计

c++ ×1

c++11 ×1

sfinae ×1

templates ×1

variadic-templates ×1