c++ - 如何使用带有模板参数和参数包的 enable if?

标签 c++ templates template-meta-programming sfinae generic-programming

我在使用 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(intel 编译器 19.0.1)编译。编译器不支持std::enable_if吗sfinae 还是英特尔编译器本身的错误?

最佳答案

1) Why parameter pack used in EnableIf()>.... If I remove the ... then the code fails in the compilation. I am unable to deduce the exact error from the trace and why this ... is needed at all there? (compiled with GCC 7.4.0).

EnableIf<IsTradingSymbol<T>()> .

template <bool b>
using EnableIf = typename std::enable_if<b, int>::type;

int也是如此当条件为 true , 没有别的。

假设条件IsTradingSymbol<T>()是真的;所以

template <Type T, EnableIf<IsTradingSymbol<T>()>...>
bool GetErrorForUi(Type A) {
  return true;
}

成为

template <Type T, int ...>
bool GetErrorForUi(Type A) {
  return true;
}

现在你有了一个模板函数。它需要一些不可从参数中推导出来的参数:a 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

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

关于c++ - 如何使用带有模板参数和参数包的 enable if?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60381705/

相关文章:

c++ - 如何从包含该类名称的字符串创建一个类的对象?

c++ - 谁能帮我解决以下编译错误——关于数组的两个实现?

c++ - 如何声明自引用模板 typedef

c++ - 这种模板编程技术的名称是什么,这是标准的 C++ 吗?

c++ - 努力实现类型列表

定义中的 C++ 类构造函数?

c++ - 如何在不打开串口的情况下使用boost创建串口

c++ - 设置的最低有效位的位置

c++ - boost 信号 - 作为参数传递的类型

c++ - 获取成员的类型