在 C++ 类中有条件地启用构造函数

ena*_*one 3 c++ constructor enable-if

我正在学习如何使用std::enable_if,到目前为止,我在有条件地启用和禁用课程中的方法方面取得了一定程度的成功。我针对布尔值对方法进行模板化,并且此类方法的返回类型是std::enable_if此类布尔值。这里的最小工作示例:

#include <array>
#include <iostream>
#include <type_traits>

struct input {};
struct output {};

template <class io> struct is_input { static constexpr bool value = false; };

template <> struct is_input<input> { static constexpr bool value = true; };

template <class float_t, class io, size_t n> class Base {
private:
  std::array<float_t, n> x_{};

public:
  Base() = default;
  Base(std::array<float_t, n> x) : x_(std::move(x)) {}
  template <class... T> Base(T... list) : x_{static_cast<float_t>(list)...} {}

  // Disable the getter if it is an input class
  template <bool b = !is_input<io>::value>
  typename std::enable_if<b>::type get(std::array<float_t, n> &x) {
    x = x_;
    }

  // Disable the setter if it is an output class
  template <bool b = is_input<io>::value>
  typename std::enable_if<b>::type set(const std::array<float_t, n> &x) {
    x_ = x;
    }
};

int main() {
  Base<double, input, 5> A{1, 2, 3, 4, 5};
  Base<double, output, 3> B{3, 9, 27};

  std::array<double, 5> a{5, 6, 7, 8, 9};
  std::array<double, 3> b{1, 1, 1};

  // A.get(a);   Getter disabled for input class
  A.set(a);

  B.get(b);
  // B.set(b);   Setter disabled for input class

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,我不能将此过程应用于有条件地启用构造函数,因为它们没有返回类型。我曾尝试针对 的值进行模板化,std::enable_if但该类无法编译:

template <class io> class Base{
private:
  float_t x_;
public:
  template <class x = typename std::enable_if<std::is_equal<io,input>::value>::type> Base() : x_{5.55} {}
}
Run Code Online (Sandbox Code Playgroud)

编译器错误如下所示:

In instantiation of ‘struct Base<output>’ -- no type named ‘type’ in  ‘struct std::enable_if<false, void>’
Run Code Online (Sandbox Code Playgroud)

正如在另一篇文章中所解释,当一个类模板被实例化时,它会实例化它的所有成员声明(尽管不一定是它们的定义)。该构造函数的声明格式错误,因此无法实例化该类。

你会如何规避这个问题?我感谢任何帮助:)

编辑:

我想要以下内容:

struct positive{};
struct negative{};

template <class float_t, class io> class Base{
private:
  float_t x_;
public:
  template <class T = io, typename = typename std::enable_if<
                          std::is_same<T, positive>::value>::type>
  Base(x) : x_(x) {}

  template <class T = io, typename = typename std::enable_if<
                          std::is_same<T, negative>::value>::type>
  Base(x) : x_(-x) {}
Run Code Online (Sandbox Code Playgroud)

如果那是可能的。

Jar*_*d42 5

构造函数有两种方法,因为您不能在返回类型中使用它

默认模板参数:

template <class io> class Base
{
private:
    float_t x_;
public:
    template <class T = io,
              typename std::enable_if<std::is_equal<T, input>::value, int>::type = 0>
    Base() : x_{5.55} {}
};
Run Code Online (Sandbox Code Playgroud)

默认参数:

template <class io> class Base{
private:
    float_t x_;
public:
    template <class T = io>
    Base(typename std::enable_if<std::is_equal<T, input>::value, int>::type = 0) :
        x_{5.55}
    {}
};
Run Code Online (Sandbox Code Playgroud)

您不能在您的情况下使用 SFINAE 作为参数仅依赖于您的类参数而不是函数的模板参数。

为了:

template <class T = io, typename = typename std::enable_if<
                      std::is_same<T, positive>::value>::type>
Base(x) : x_(x) {}

template <class T = io, typename = typename std::enable_if<
                      std::is_same<T, negative>::value>::type>
Base(x) : x_(-x) {}
Run Code Online (Sandbox Code Playgroud)

你犯了一个常见的错误:默认模板参数不是签名的一部分,所以你只声明和定义两次

template <class, typename>
Base::Base(x);
Run Code Online (Sandbox Code Playgroud)

这就是为什么我用

typename std::enable_if<std::is_same<T, input>::value, int>::type = 0
Run Code Online (Sandbox Code Playgroud)

其中 type 在 的左侧=,因此您将有 2 种不同的类型,因此有 2 种不同的签名。

C++20 添加requires允许简单地丢弃方法:

template <class io> class Base
{
private:
    float_t x_;
public:
    template <class T = io,
              typename std::enable_if<std::is_equal<T, input>::value, int>::type = 0>
    Base() : x_{5.55} {}
};
Run Code Online (Sandbox Code Playgroud)