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



我正试图编写一个类,该类根据类自己的模板参数的值公开不同的构造函数。尝试这样做时,脑海中浮现的天真代码如下:

// 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按预期工作,但不知何故,不应被视为错误的是。有什么线索可以说明这一切吗?

为了使用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;
}

SFINAE使用函数模板的模板参数;你应该让构造函数模板拥有自己的模板参数,并用std::enable_if而不是类模板参数来检查它们;否则,通过某些类模板实例化,所有构造函数模板都将被实例化并导致错误。

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

实时

和C++20版本:

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

最新更新