【发布时间】:2015-08-07 01:27:51
【问题描述】:
这绝对是一个微不足道的问题,但我不知道该怎么做。
我有一个模板函数,比如template <unsigned int N> void my_function()。现在,我对my_function 有两种不同的实现,如果N 大于例如100,则应使用第一种,如果N 小于此,则应使用另一种。
我尝试像这样使用 SFINAE:
template <unsigned int N, typename = enable_if <N >= 100> :: type> my_function()
{
// First implementation
}
template <unsigned int N, typename = enable_if <N < 100> :: type> my_function()
{
// Second implementation
}
但那是两次声明同一个函数。然后我尝试做类似的事情
template <unsigned int N, bool = (N >= 100)> my_function();
然后用两个不同的布尔值实现这两个函数。没有成功,因为它是部分专业化。
然后我尝试将N包装为结构参数,并在函数调用中使用bool,但它在专门化类之前专门化了成员函数,这是无法做到的。
有没有合理的方法来做到这一点?
【问题讨论】:
标签: c++ templates c++11 sfinae