如何根据特定类型任意启用或禁用class方法?

How to arbitrarily enable or disable class method based on specific type?

我有一个 class 这样的:

struct X
{
    enum Type { INT, FLOAT };
    using val_t = std::tuple<int, float>;

    X(Type t) : type(t) {}

    Type type;

    template<typename T>
    X& operator =(T x)
    {
        // ???
        static_assert(T is the same as `type');

        // ???
        std::get<type>(val) = x;

        return *this;
    }

    val_t val;
};

如果用户尝试分配不兼容的值,是否可以在编译时断言?

例如:

X x1(X::INT);
x1 = 5; // OK
x1 = 3.14; // compilation error

注意:我更喜欢将 class 保留为模板,因为我需要将其实例保留在集合中(如 std::vector 等)。

考虑到您有 Type type; 如果 type 是 INT 或 FLOAT 或您拥有的任何东西,您不能在编译时断言。对于该检查,您只能在运行时断言。

对于其他所有内容,您可以进行编译时检查,并使用一些模板元编程进行运行时检查:

namespace detail_tuple
{
    template <typename T, std::size_t N, typename... ARGS>
    struct get_by_type_impl {
        enum {
            kIdx = N
        };
    };
    template <typename T, std::size_t N, typename... ARGS>
    struct get_by_type_impl<T, N, T, ARGS...> {
        enum {
            kIdx = N
        };
    };
    template <typename T, std::size_t N, typename U, typename... ARGS>
    struct get_by_type_impl<T, N, U, ARGS...> {
        enum {
            kIdx = get_by_type_impl<T, N + 1, ARGS...>::kIdx
        };
    };
}
template <typename, typename>
struct validator;
template <typename T, typename... ARGS>
struct validator < T, std::tuple<ARGS...> >
{
    static void validate(const std::size_t type_idx)
    {
        //compiletime checks
        //get index of type T in ARGS...
        constexpr auto ind = detail_tuple::get_by_type_impl<T, 0, ARGS...>::kIdx;
        //check if index is valid
        static_assert(ind < sizeof...(ARGS), "Type index out of bounds, type T is was not found in the tuple!");

        //runtime checks
        if (type_idx != ind)
            std::cout << "Incompatible type index!\n";
    }
};

struct X
{
    enum Type
    {
        INT = 0,
        FLOAT,
        TYPE_COUNT,
    };
    using val_t = std::tuple<int, float>;


    X(Type t) : type(t) {}

    Type type;

    template<typename T>
    X& operator =(const T& x)
    {
        validator<T, val_t>::validate(type);

        std::get<T>(val) = x;
        return *this;
    }

    val_t val;
};

因为 std::get 使用类型 T 而不是 type

不能:type_的值为运行时间数据,编译错误未在运行时间判断。

你可以这样做:

enum Type { INT, FLOAT };
template<Type type_>
struct X {
  using val_t = std::tuple<int, float>;


  template<typename T>
  X& operator =(T x) {
    // ???
    static_assert((type_==INT&&std::is_same<T,int>{})||(type_==FLOAT&&std::is_same<T,float>{}),
      "types do not match"
    );
    std::get<T>(val) = x;

    return *this;
  }

  val_t val;
};
X<INT> x1;
x1 = 5; // OK
x1 = 3.14; // compilation error

但我看不出有什么意义。

一种方法是拥有一个不进行检查的基类型,它只存储状态,而派生类型知道它的类型。

struct Base{
  enum {INT,FLOAT} Type;
  // etc
};
template<Base::Type type>
struct Derived:private Base{
  Derived():Base(type){}
  using Base::some_method; // expose base methods
  Base& get_base()&{return *this;}
  Base get_base()&&{return std::move(*this);}
  Base const& get_base()const&{return *this;}

  template<class T>
  Derived& operator=( T o){
    static_assert((type_==INT&&std::is_same<T,int>{})||(type_==FLOAT&&std::is_same<T,float>{}),
      "types do not match"
    );
    Base::operator=(std::move(o));
    return *this;
  }
};

Base 不检查,充其量它 运行 时间断言。 Derived 在编译时检查。

Niw 当你在编译时静态知道你使用的类型时 Derived<INT> d;;当您不需要或需要忘记时,请使用 .get_base()Base b(type_enum_val);.