如何将enable-if与模板参数和参数包一起使用

How to use enable if with template arguments and parameter pack?

本文关键字:参数 一起 包一起 enable-if      更新时间:2023-10-16

我在处理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_ifsfinae,或者是英特尔编译器本身的错误?

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()需要两个模板参数:Typeint。正好一个int,不再是零或更多。

现在一个整数是强制性的,只有一个是可接受的。

所以现在呼叫

GetErrorForUi<Type::First>(Type::First);

不起作用(给出编译错误(,因为您没有传递强制模板int参数。

还有

GetErrorForUi<Type::First, 0, 1>(Type::First);

不起作用(删除省略号之后;应该编译之前(,因为函数只需要一个整数,而您传递了两个ints。

这也应该回答你的第二点。

传统方式是:

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(,并且这些过载将被丢弃