SFINAE根据类值模板参数选择构造函数

Matrefeytontias:

我正在尝试编写一个类,该类根据类自己的模板参数的值公开不同的构造函数。试图做到这一点的天真的代码如下:

// C++14
#include <type_traits>

template <int compile_time_w = -1, int compile_time_h = -1>
struct Grid
{
    template <std::enable_if_t<compile_time_w < 0 && compile_time_h < 0, int> = 0>
    Grid(int runtime_w, int runtime_h) : _w(runtime_w), _h(runtime_h) {}

    template <std::enable_if_t<compile_time_w < 0 && compile_time_h >= 0, int> = 0>
    Grid(int runtime_w) : _w(runtime_w), _h(compile_time_h) {}

    template <std::enable_if_t<compile_time_w >= 0 && compile_time_h < 0, int> = 0>
    Grid(int runtime_h) : _w(compile_time_w), _h(runtime_h) {}

    template <std::enable_if_t<compile_time_w >= 0 && compile_time_h >= 0, int> = 0>
    Grid() : _w(compile_time_w), _h(compile_time_h) {}

    int _w, _h;
};

int main()
{
    // Grid<2, 2> grid; // any combination of template parameters + constructor parameters fails to compile

    return 0;
}

在没有任何实例化的情况下编译类可以正常工作,但是尝试以任何方式或容量实例化该类始终会失败。编译错误总是具有相同的格式,并且会向SFINAE应触发的每个构造函数报告该错误:

error: no type named ‘type’ in ‘struct std::enable_if’

显然,std::enable_if它按预期工作,但是某种程度上不应视为错误。关于这一切的任何线索吗?

内森·奥利弗(NathanOliver):

为了使用SFINAE,模板参数必须是当前模板的一部分。由于compile_time_wcompile_time_h是类模板参数的一部分,因此它们不可用。要修复它,请将它们添加到每个功能模板,例如

template <int compile_time_w = -1, int compile_time_h = -1>
struct Grid
{
    template <int compile_time_w_l = compile_time_w, int compile_time_h_l = compile_time_h, std::enable_if_t<compile_time_w_l < 0 && compile_time_w_l < 0, int> = 0>
    Grid(int runtime_w, int runtime_h) : _w(runtime_w), _h(runtime_h) {}

    template <int compile_time_w_l = compile_time_w, int compile_time_h_l = compile_time_h, std::enable_if_t<compile_time_w_l < 0 && compile_time_w_l >= 0, int> = 0>
    Grid(int runtime_w) : _w(runtime_w), _h(compile_time_h) {}

    template <int compile_time_w_l = compile_time_w, int compile_time_h_l = compile_time_h, std::enable_if_t<compile_time_w_l >= 0 && compile_time_w_l < 0, int> = 0>
    Grid(int runtime_h) : _w(compile_time_w), _h(runtime_h) {}

    template <int compile_time_w_l = compile_time_w, int compile_time_h_l = compile_time_h, std::enable_if_t<compile_time_w_l >= 0 && compile_time_w_l >= 0, int> = 0>
    Grid() : _w(compile_time_w), _h(compile_time_h) {}

    int _w, _h;
};

int main()
{
    Grid<2, 2> grid; // any combination of template parameters + constructor parameters fails to compile

    return 0;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

通过SFINAE在模板参数中选择构造函数

根据模板参数选择合适的复制构造函数

值包装器的可变参数模板构造函数的类构造函数优先级

根据模板参数添加副本构造函数

继承的类模板的参数化构造函数

如何从类构造函数传递模板参数

带有构造函数参数的模板类

专门针对模板化模板参数的模板类构造函数

根据template参数有条件地定义模板类的构造函数

具有左值和右值的可变参数模板类构造函数

我可以根据模板参数将某个值传递给成员构造函数吗?

SFINAE用于构造函数参数

SFINAE可变参数构造函数

非模板类的构造函数中的模板参数

定义类模板构造函数并提供模板参数

以模板化类作为参数的可变类构造函数

c ++模板类根据类型通过ref传递构造函数

模板类并根据迭代调用不同的构造函数

C++在构造函数中选择类成员的模板类型

构造函数接受参数时如何实例化模板类

嵌套类构造函数中的父模板参数推导

用于构造函数的类模板参数包扩展

限制可变参数模板类中的构造函数访问

从传递给构造函数的类作为模板参数继承

如果给定的模板参数提供了类型定义,则启用具有sfinae的构造函数

C ++ SFINAE双嵌套的initializer_list与可变参数模板构造函数

根据构造函数参数将父类更改为子类

根据构造函数参数的类方法的特殊实现

表达式SFINAE:如何根据类型是否包含带有一个或多个参数的函数来选择模板版本