是否有通用模板/宏用于测试例如。如果名字被定义了,那就是ala。is_transparent是如何工作的。
is_transparent使比较器对std::set透明(即。可以使用自定义类型查找/等)。它只需要被定义成任何东西,例如。using is_transparent = void;
Im希望对某些自定义类型执行类似的操作,但理想情况下,我应该使用来自标准或助推的一些东西(甚至是宏),或者我可以使用有关实现的指导。
问题是,如何测试类型是否已定义(是否存在?)基于限定名?
发布于 2019-12-19 14:37:59
使用检测成语:
#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;https://stackoverflow.com/questions/59411729
复制相似问题