如何检测例如。 T::is_transparent是定义?
How detect if eg. T::is_transparent is defined?
是否有通用的 template/Macro 用于测试,例如。如果定义了名称,那是什么。 is_transparent
是如何工作的。
is_transparent
使 比较器 对 std::set
透明(即可以 lookup/etc。使用自定义类型)。它只需要定义为任何内容,例如。 using is_transparent = void;
我希望为某些自定义类型做类似的事情,但理想情况下我会使用来自 std or boost 的东西(甚至是宏),或者我可以使用实施指南。
问题是,如何根据限定名称测试类型是否定义(存在?)?
使用检测成语:
#include <iostream>
#include <experimental/type_traits>
struct A {};
struct B
{
using is_transparent = void; // Any other type works too.
};
template <typename T> using detect_transparent = typename T::is_transparent;
int main()
{
std::cout << std::experimental::is_detected_v<detect_transparent, A> << '\n'; // 0
std::cout << std::experimental::is_detected_v<detect_transparent, B> << '\n'; // 1
}
is_detected_v
是所谓的 library fundamentals TS v2.
的实验性特征
如果你的编译器不支持,或者你不喜欢在代码中看到experimental
这个词,你可以自己实现:
namespace impl
{
template <typename T, typename ...P>
struct dependent_type {using type = T;};
// `std::void_t` used to be broken in Clang (probably it no longer is),
// so I use a custom safe replacement instead.
template <typename A, typename ...B>
using void_type = typename dependent_type<void, A, B...>::type;
template <typename DummyVoid, template <typename...> typename A, typename ...B>
struct is_detected : std::false_type {};
template <template <typename...> typename A, typename ...B>
struct is_detected<void_type<A<B...>>, A, B...> : std::true_type {};
}
template <template <typename...> typename A, typename ...B>
inline constexpr bool is_detected_v = impl::is_detected<void, A, B...>::value;
是否有通用的 template/Macro 用于测试,例如。如果定义了名称,那是什么。 is_transparent
是如何工作的。
is_transparent
使 比较器 对 std::set
透明(即可以 lookup/etc。使用自定义类型)。它只需要定义为任何内容,例如。 using is_transparent = void;
我希望为某些自定义类型做类似的事情,但理想情况下我会使用来自 std or boost 的东西(甚至是宏),或者我可以使用实施指南。
问题是,如何根据限定名称测试类型是否定义(存在?)?
使用检测成语:
#include <iostream>
#include <experimental/type_traits>
struct A {};
struct B
{
using is_transparent = void; // Any other type works too.
};
template <typename T> using detect_transparent = typename T::is_transparent;
int main()
{
std::cout << std::experimental::is_detected_v<detect_transparent, A> << '\n'; // 0
std::cout << std::experimental::is_detected_v<detect_transparent, B> << '\n'; // 1
}
is_detected_v
是所谓的 library fundamentals TS v2.
如果你的编译器不支持,或者你不喜欢在代码中看到experimental
这个词,你可以自己实现:
namespace impl
{
template <typename T, typename ...P>
struct dependent_type {using type = T;};
// `std::void_t` used to be broken in Clang (probably it no longer is),
// so I use a custom safe replacement instead.
template <typename A, typename ...B>
using void_type = typename dependent_type<void, A, B...>::type;
template <typename DummyVoid, template <typename...> typename A, typename ...B>
struct is_detected : std::false_type {};
template <template <typename...> typename A, typename ...B>
struct is_detected<void_type<A<B...>>, A, B...> : std::true_type {};
}
template <template <typename...> typename A, typename ...B>
inline constexpr bool is_detected_v = impl::is_detected<void, A, B...>::value;