如何检测是否例如.T::is_transparent 是定义的?



是否有用于测试的通用模板/宏,例如。 如果定义了名称,则is_transparent如何工作。

is_transparent使比较器std::set透明(即可以使用自定义类型查找/等(。它只需要定义为任何东西,例如。using is_transparent = void;

我希望为某些自定义类型做类似的事情,但理想情况下,我会使用 std 或 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是所谓的库基础 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;

最新更新