为什么C ++编译器会抱怨禁用一元运算符?

时间:2020-07-31 07:29:35

标签: c++ templates operator-overloading template-meta-programming

我希望能够基于这样的模板参数禁用重载的运算符功能

template <int T>
 class Foo {
  int v;

  template <std::enable_if_t<T == 0, int> = 0>
  Foo operator-() const
  {
    return {-v};
  }
};

但是当我尝试实例化一个Foo的值时,该值会禁用该功能(即1),编译器会抱怨

Foo<0> f0; // This compiles
Foo<1> f1; // This does not

我不是要调用禁用的功能,而是编译器抱怨无法访问它。

对此行为有任何解释吗?

0 个答案:

没有答案