如何将enable-if与模板参数和参数包一起使用
How to use enable if with template arguments and parameter pack?
我在处理c++模板时,发现了这段使用SFINAE和std::enable_if
的代码。我在使用此代码时遇到两个问题。
#include <string>
#include <iostream>
enum class Type : int { First = 1, Second, Third };
template <Type T>
struct Symbol : public std::true_type {
using Parent = std::true_type;
using type = Parent::value_type;
static constexpr type value = Parent::value;
static constexpr type GetValue() {
if constexpr (T == Type::First) return value;
else return !value;
}
};
template <bool b>
using EnableIf = typename std::enable_if<b, int>::type;
template <Type T> static constexpr bool IsTradingSymbol() {
return Symbol<T>::GetValue();
}
template <Type T, EnableIf<IsTradingSymbol<T>()>...>
bool GetErrorForUi(Type A) {
return true;
}
template <Type T, EnableIf<!IsTradingSymbol<T>()>...>
bool GetErrorForUi(Type A) {
return false;
}
int main () {
std::cout << GetErrorForUi<Type::First>(Type::First) << std::endl;
std::cout << GetErrorForUi<Type::Second>(Type::Second) << std::endl;
return 0;
}
1(EnableIf<IsTradingSymbol<T>()>...
中为什么使用参数包。如果我删除...
,那么代码在编译中失败。我无法从跟踪中推断出确切的错误,为什么需要这个...
?(根据GCC 7.4.0编制(。
enableif.cpp: In function ‘int main()’:
enableif.cpp:43:54: error: no matching function for call to ‘GetErrorForUi<First>(Type)’
std::cout << GetErrorForUi<Type::First>(Type::First) << std::endl;
^
enableif.cpp:26:6: note: candidate: template<Type T, typename std::enable_if<IsTradingSymbol<T>(), int>::type <anonymous> > bool GetErrorForUi(Type)
bool GetErrorForUi(Type A) {
^~~~~~~~~~~~~
enableif.cpp:26:6: note: template argument deduction/substitution failed:
enableif.cpp:43:54: note: couldn't deduce template parameter ‘<anonymous>’
std::cout << GetErrorForUi<Type::First>(Type::First) << std::endl;
^
enableif.cpp:30:6: note: candidate: template<Type T, typename std::enable_if<(! IsTradingSymbol<T>()), int>::type <anonymous> > bool GetErrorForUi(Type)
bool GetErrorForUi(Type A) {
^~~~~~~~~~~~~
enableif.cpp:30:6: note: template argument deduction/substitution failed:
enableif.cpp:43:54: note: couldn't deduce template parameter ‘<anonymous>’
std::cout << GetErrorForUi<Type::First>(Type::First) << std::endl;
^
enableif.cpp:44:56: error: no matching function for call to ‘GetErrorForUi<Second>(Type)’
std::cout << GetErrorForUi<Type::Second>(Type::Second) << std::endl;
^
enableif.cpp:26:6: note: candidate: template<Type T, typename std::enable_if<IsTradingSymbol<T>(), int>::type <anonymous> > bool GetErrorForUi(Type)
bool GetErrorForUi(Type A) {
^~~~~~~~~~~~~
enableif.cpp:26:6: note: template argument deduction/substitution failed:
enableif.cpp:44:56: note: couldn't deduce template parameter ‘<anonymous>’
std::cout << GetErrorForUi<Type::Second>(Type::Second) << std::endl;
^
enableif.cpp:30:6: note: candidate: template<Type T, typename std::enable_if<(! IsTradingSymbol<T>()), int>::type <anonymous> > bool GetErrorForUi(Type)
bool GetErrorForUi(Type A) {
^~~~~~~~~~~~~
enableif.cpp:30:6: note: template argument deduction/substitution failed:
enableif.cpp:44:56: note: couldn't deduce template parameter ‘<anonymous>’
std::cout << GetErrorForUi<Type::Second>(Type::Second) << std::endl;
2( 该代码不是使用icc X86-64(英特尔编译器19.0.1(编译的。该编译器是否不支持std::enable_if
sfinae,或者是英特尔编译器本身的错误?
1(为什么在EnableIf((中使用参数包>。。。。如果我删除。。。则代码在编译中失败。我无法从痕迹中推断出确切的错误,以及为什么。。。那里需要什么?(根据GCC 7.4.0编制(。
查看EnableIf<IsTradingSymbol<T>()>
。
它是
template <bool b>
using EnableIf = typename std::enable_if<b, int>::type;
当条件为true
时,int
也是,除此之外什么都没有。
假设条件IsTradingSymbol<T>()
为真;所以
template <Type T, EnableIf<IsTradingSymbol<T>()>...>
bool GetErrorForUi(Type A) {
return true;
}
成为
template <Type T, int ...>
bool GetErrorForUi(Type A) {
return true;
}
现在您有了一个模板函数。它需要一些不可从参数中推断的参数:一个强制性的Type
(T
(,以及一个零或更多的整数列表。
您调用函数如下
GetErrorForUi<Type::First>(Type::First);
因此,您只将一个Type
作为模板参数传递给函数。非整数。
这是因为函数需要零或更多整数。
但当您删除省略号(...
(时,函数将变为
template <Type T, int>
bool GetErrorForUi(Type A) {
return true;
}
现在GetErrorForUi()
需要两个模板参数:Type
和int
。正好一个int
,不再是零或更多。
现在一个整数是强制性的,只有一个是可接受的。
所以现在呼叫
GetErrorForUi<Type::First>(Type::First);
不起作用(给出编译错误(,因为您没有传递强制模板int
参数。
还有
GetErrorForUi<Type::First, 0, 1>(Type::First);
不起作用(删除省略号之后;应该编译之前(,因为函数只需要一个整数,而您传递了两个int
s。
这也应该回答你的第二点。
传统方式是:
template <Type T, std::enable_if_t<condition_v<T>, int> = 0>
bool GetErrorForUi(Type A);
而不是
template <Type T, std::enable_if_t<condition_v<T>, int>...>
bool GetErrorForUi(Type A);
所以在替换之后,就是
template <Type T, int /*unnammed*/ = 0>
bool GetErrorForUi(Type A);
template <Type T, int /*unnammed*/...>
bool GetErrorForUi(Type A);
如果发生替换失败,则不是错误(SFINAE(,并且这些过载将被丢弃
- 如何将enable-if与模板参数和参数包一起使用
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 概念可以与模板模板参数一起使用吗?
- 为什么当我尝试将priority_queue与参数一起使用作为指向结构的指针时会弹出错误
- 标准::原子::compare_exchange与两个memory_order参数一起使用的真实示例
- 如何在 C++ 中将 typedef 与类初始值设定项参数一起使用?
- 我可以将"token pasting operator"与"const"模板参数一起使用吗?
- 哪个强制转换应与模板类参数一起使用,dynamic_cast或reinterpet_cast?
- 将 boost::function 与指向派生类的共享指针的参数一起使用
- 如何将 #ifndef 与宏参数一起使用
- 如何将初始值设定项与右值引用参数一起使用 // 为什么不能使用另一个 C 样式数组变量初始化 C 样式数组
- 将模板类与引用非类型模板参数一起使用时出现链接器错误
- 如何将QDebug()与QT中的两个参数一起使用
- 将函数与指针参数一起使用作为另一个函数的参数
- 是否可以在C++中将宏条件与模板参数一起使用
- 提升program_options解析选项和参数一起
- 在C++中,如何仅将长选项与必需参数一起使用
- 当通过引用指针将函数与参数一起使用时会发生什么
- 将typedef与构造函数参数一起使用
- 将函数指针上的参数与默认值参数一起丢弃是否有效