【发布时间】:2018-04-01 02:57:22
【问题描述】:
当模板构造函数的参数类型与类型“MyClass”匹配时,我正在尝试使用 std::enable_if 禁用模板类的模板构造函数,以便我可以使用其他构造函数,它允许我用另一个模板的类初始化当前模板的类。
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;
};
【问题讨论】:
-
对不起...“如果 Args = MyClass 我想禁用此功能”是指“如果所有 Args 都等于 MyClass”吗?或者“如果 Args 之一等于 MyClass”?还是什么?
-
@max66 它没有以最清晰的方式编写,但是如果
sizeof...(Args) == 1和Args[0] "==" MyClass<t, anySize>,OP希望禁用可变参数构造函数。基本上,OP 希望其他构造函数具有比可变参数更高的优先级 -
@Justin - 我明白了...谢谢。
-
标签: c++ c++11 templates variadic-templates sfinae