I'm trying to write a class that exposes different constructors depending on the value of the class's own template parameters. The naive code that came to mind trying to do this is as follows :
// 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;
}
Compiling the class without any instantiation of it works fine, but trying to instantiate it in any way or capacity always fails. The compilation error is always of the same format, and is reported for every constructor where SFINAE should trigger :
error: no type named ‘type’ in ‘struct std::enable_if’
Apparently std::enable_if
is working as intended, but somehow what should not be considered as an error is. Any clue on what this is all about ?