根据模板参数不等式执行不同的函数
Execute different functions depending on template parameter disequation
这绝对是一个微不足道的问题,但我不知道如何做到这一点。
我有一个模板函数,比如template <unsigned int N> void my_function()
。现在,我对my_function
有两种不同的实现,如果N
大于100,则应该使用第一种实现,如果N
小于100,则应该使用另一种实现。
我尝试像这样使用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
包装为struct参数和函数调用中的bool,但它在特化类之前特化了一个成员函数,这是不能做的。
是否有合理的方法来做到这一点?
试试这个:
#include <type_traits>
#include <iostream>
template <unsigned int N, typename std::enable_if <N >= 100> :: type* = nullptr>
void my_function()
{
std::cout << "N >= 100" << std::endl;
}
template <unsigned int N, typename std::enable_if <N < 100> :: type* = nullptr>
void my_function()
{
std::cout << "N < 100" << std::endl;
}
int main()
{
my_function<42>();
my_function<100>();
}
模板默认参数不参与重载(因此SFINAE不适用)。另一方面,在上面的代码片段中,依赖模板的非类型参数位于赋值的左侧,因此SFINAE开始起作用。
如果您出于某种原因不喜欢enable_if,您可以选择标签调度:
#include <type_traits>
class low {};
class high {};
template <int N, class T>
void func(T, low)
{
// version for high N
}
template <int N, class T>
void func(T, high)
{
// version for low N
}
template <int N, class T>
void func(T val)
{
func<N>(val, std::conditional_t<(N>=100), high, low>{});
}
int main()
{
func<3>(3.14159); // low version
func<256>("Yo"); // high version
}
在这种情况下,我们可以将标记限制为简单的东西,如true_type和谬误,但通常这可能是另一种方法。
您可以对返回类型使用SFINAE:
template <unsigned int N>
enable_if_t<(N >= 100)> my_function()
{
// First implementation
}
template <unsigned int N>
enable_if_t<(N < 100)> my_function()
{
// Second implementation
}
目前,您只有template <unsigned int N, typename T>
与T
的默认类型不同。
对于部分专门化,您可以转发到结构:
template <unsigned int N, bool = (N >= 100)>
struct my_function_impl;
template <unsigned int N>
struct my_function_impl<N, true>
{
void operator () const { /* First implementation */}
};
template <unsigned int N>
struct my_function_impl<N, false>
{
void operator () const { /* Second implementation */}
};
template <unsigned int N>
void my_function() { my_function_impl<N>{}(); }
相关文章:
- 如何以及何时使用 getline 函数执行计算?
- 主函数执行时C++堆栈溢出异常
- 如何编写在函数执行过程中垃圾回收的测试用例?
- 插入到映射中的元素在函数执行后清除
- 遵循 C++ 中的构造函数执行顺序
- 成员函数执行时成员变量的增量
- 关于如何在函数执行后使用和获取返回值的问题
- 从 C 代码 system() 函数执行的 Linux 命令与从终端执行时给出的结果不同
- 顺序异构函数执行器
- 静态 LPTSTR 变量在函数执行后丢失值
- C++如何使函数执行得更快
- C 中函数执行的优先顺序
- 函数执行时间
- 当类成员函数执行时,程序停止工作
- 主函数执行之前的C++segfault
- 如何使用以查看<chrono>函数执行所花费的时间
- 如何从另一个函数执行一个函数
- 函数执行一次
- 为我自己的函数执行通常的算术转换
- 如何对继承构造函数执行正确的SFINAE