解析歧义模板
Resolve ambiguous template
本文关键字:歧义 更新时间:2023-10-16
我想知道是否有可能解决这个模糊的模板函数:
//function1
template<typename returnType>
returnType call()
{
//function with return type
}
//function2
template<typename var>
void call()
{
//function without return type
}
call<int>(); //call function1
call<void>(); //call function2
我想防止以下解决方案:
//function1
template<typename returnType>
returnType call()
{
//function with return type
}
//function2
void call()
{
//function without
}
call<int>(); //call function1
call(); //call function2
您可以显式专门化void
的模板:
//function2
template<>
void call<void>()
{
//function without return type
}
我不确定我是否明白了这一点,但我尝试了一下SFINAE:
//function1
template<typename returnType>
typename std::enable_if
<
!std::is_same< returnType, void >::value,
returnType
>::type call()
{
//function with return type
}
//function2
template<typename var>
typename std::enable_if
<
std::is_same< var, void >::value
>::type call()
{
//function without return type
}
相关文章:
- 构造对象的歧义
- 我知道函数调用中存在歧义.有没有办法调用foo()函数
- 数组初始值设定项的构造函数歧义
- 消除好友和成员二进制运算符的歧义
- 用' . '代替' :: '会在C++中造成歧义吗?
- 为什么下面带有非常量转换函数的代码没有歧义?
- std::bind 是否实现了 std::ref 和 std::cref 来消除函数调用的歧义?
- 使用 bool 和 const char 重载的 C++ 函数会在没有警告的情况下产生歧义 (MSVC2012)
- C++SFINAE enable_if_t成员函数,如何消除歧义?
- 多重继承相同的方法名,没有歧义
- 如何在模板化转换运算符中消除此构造的歧义?
- 当存在覆盖歧义函数时,代码如何运行?
- 如何区分宏函数和函数函数而没有任何歧义?
- C++17 中的歧义错误(模板模板参数和默认参数问题)
- 模板与常规函数歧义 - UB?
- C++ 类的构造函数和函数调用运算符 () 重载之间的歧义
- 歧义语法是如何解决的
- 使用enable_if解决多重继承歧义
- 通过继承重载运算符会导致歧义
- 如何解决传递给boost线程的函数中的歧义