This question already has answers here:
enable_if with copy constructors

(2个答案)


3年前关闭。




有人可以帮助我了解为什么以下代码无法编译:
#include <type_traits>

class A{};
class B{};


template< typename T >
class C
{
  template< typename = std::enable_if_t<std::is_same<T, A>::value > >
  void foo()
  {}

  template< typename = std::enable_if_t<std::is_same<T, B>::value > >
  void foo()
  {}
};

错误信息:
t.cpp:15:8: error: class member cannot be redeclared
  void foo()
       ^
t.cpp:11:8: note: previous declaration is here
  void foo()
       ^
1 error generated.

我期望总是只有一个foo定义处于 Activity 状态;在T的情况下,第一个等于A;在T的情况下,第二个等于B

如果有人可以帮助我修复代码,那就太好了。

最佳答案

直接来自cppreference,重点是:



用简单的话来说,这两个函数签名是:

template<typename>
void foo()

关于c++ - 使用enable_if隐藏函数定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46614654/

10-09 06:33
查看更多