相关疑难解决方法(0)

C++20:使用概念实现 std::is_constructible

是否有一种可移植的方法来std::is_constructible使用概念来实现而无需仅STL使用requires表达式或模板元编程?

考虑这段代码:

template <class T, class... Args>
struct is_constructible
    : std::bool_constant<requires {
      new T(std::declval<Args>()...);
    }> {
};
Run Code Online (Sandbox Code Playgroud)

它适用于除引用之外的其他数据类型,因为不能使用new引用类型。

// Test cases
auto main() -> int {
  static_assert(is_constructible<int>::value);
  static_assert(is_constructible<int, int>::value);
  static_assert(is_constructible<int, float>::value);
  static_assert(!is_constructible<int, int *>::value);
  static_assert(is_constructible<int &, int &>::value);
  static_assert(is_constructible<int &&, int &&>::value);
  static_assert(!is_constructible<void, void>::value);
  static_assert(!is_constructible<int(), int()>::value);
  static_assert(is_constructible<int (*)(), int()>::value);
  static_assert(!is_constructible<intptr_t, int *>::value);
  static_assert(!is_constructible<int &, float &>::value);

  static_assert(std::is_constructible<int>::value);
  static_assert(std::is_constructible<int, int>::value);
  static_assert(std::is_constructible<int, float>::value);
  static_assert(!std::is_constructible<int, int *>::value);
  static_assert(std::is_constructible<int &, int &>::value);
  static_assert(std::is_constructible<int &&, int &&>::value); …
Run Code Online (Sandbox Code Playgroud)

c++ type-traits c++-concepts c++20

1
推荐指数
1
解决办法
414
查看次数

标签 统计

c++ ×1

c++-concepts ×1

c++20 ×1

type-traits ×1