我想检查类型是否来自特定的名称空间。这是我想出的:
#include <utility>
namespace helper
{
template <typename T, typename = void>
struct is_member_of_sample : std::false_type
{
};
template <typename T>
struct is_member_of_sample<
T,
decltype(adl_is_member_of_sample(std::declval<T>()))> : std::true_type
{
};
}
namespace sample
{
template <typename T>
auto adl_is_member_of_sample(T && ) -> void;
}
// -- Test it
namespace sample
{
struct X;
}
struct Y;
static_assert(helper::is_member_of_sample<sample::X>::value, "");
static_assert(not helper::is_member_of_sample<Y>::value, "");
int main(){}
只要没有人添加adl_is_member_of_sample
到自己的名称空间(甚至全局名称空间),此方法就可以正常工作。当然,我必须为要测试的每个命名空间创建这样的构造。
有没有一种更好的方法可以在编译时检查类型是否来自特定名称空间?
在EDSL中,我在编译时检查类型特征,以查看某些表达式是否有效。其中一些类型特征非常简单:如果类具有using is_numeric = void
,则将其视为数字表达式。工作正常。
is_numeric
虽然很通用。其他人也可以使用它。因此,我考虑过通过检查类型是否来自预期名称空间来支持trait。
有一种(特定于编译器的)方法可以测试某个类型是否在某个命名空间中,但是我将让您决定它是否比您的类型更好:
#include <utility>
#include <type_traits>
namespace helper
{
class ctstring
{
public:
constexpr ctstring(const char* string) : _string(string)
{
}
constexpr const char* c_str() const
{
return _string;
}
constexpr bool begins_with(const ctstring other) const
{
return !*other.c_str() ||
(*_string && *_string == *other.c_str() &&
ctstring(_string + 1).begins_with(other.c_str() + 1));
}
private:
const char* _string;
};
template <typename T>
constexpr bool is_type_in_namespace(const ctstring name)
{
#if defined(_MSC_VER)
#define PRETTY_FUNCTION_OFFSET_1 \
(sizeof("void __cdecl helper::is_type_in_namespace<struct ") - 1)
#define PRETTY_FUNCTION_OFFSET_2 \
(sizeof("void __cdecl helper::is_type_in_namespace<class ") - 1)
return ctstring(__FUNCSIG__ + PRETTY_FUNCTION_OFFSET_1).begins_with(name) ||
ctstring(__FUNCSIG__ + PRETTY_FUNCTION_OFFSET_2).begins_with(name);
#undef PRETTY_FUNCTION_OFFSET_1
#undef PRETTY_FUNCTION_OFFSET_2
#elif defined(__clang__)
return ctstring(__PRETTY_FUNCTION__ +
(sizeof("bool helper::is_type_in_namespace(const "
"helper::ctstring) [T = ") -
1))
.begins_with(name);
#elif defined(__GNUC__)
return ctstring(__PRETTY_FUNCTION__ +
(sizeof("constexpr bool "
"helper::is_type_in_namespace(helper::ctstring) "
"[with T = ") -
1))
.begins_with(name);
#else
#error "Your compiler is not supported, yet."
#endif
}
}
// -- Test it
namespace sample
{
struct True_X;
class True_Y;
template <typename>
class True_T;
template <typename A>
using True_U = True_T<A>;
}
struct False_X;
class False_Y;
template <typename>
class False_T;
template <typename A>
using False_U = False_T<A>;
void test1()
{
static_assert(helper::is_type_in_namespace<sample::True_X>("sample::"), "1");
static_assert(helper::is_type_in_namespace<sample::True_Y>("sample::"), "2");
static_assert(helper::is_type_in_namespace<sample::True_T<int>>("sample::"), "3");
static_assert(helper::is_type_in_namespace<sample::True_U<int>>("sample::"), "4");
static_assert(!helper::is_type_in_namespace<False_X>("sample::"), "5");
static_assert(!helper::is_type_in_namespace<False_Y>("sample::"), "6");
static_assert(!helper::is_type_in_namespace<False_T<int>>("sample::"), "7");
static_assert(!helper::is_type_in_namespace<False_U<int>>("sample::"), "8");
}
namespace sample
{
void test2()
{
static_assert(helper::is_type_in_namespace<True_X>("sample::"), "1");
static_assert(helper::is_type_in_namespace<True_Y>("sample::"), "2");
static_assert(helper::is_type_in_namespace<True_T<int>>("sample::"), "3");
static_assert(helper::is_type_in_namespace<True_U<int>>("sample::"), "4");
static_assert(!helper::is_type_in_namespace<::False_X>("sample::"), "5");
static_assert(!helper::is_type_in_namespace<::False_Y>("sample::"), "6");
static_assert(!helper::is_type_in_namespace<::False_T<int>>("sample::"), "7");
static_assert(!helper::is_type_in_namespace<::False_U<int>>("sample::"), "8");
}
namespace inner
{
void test3()
{
static_assert(helper::is_type_in_namespace<::sample::True_X>("sample::"), "1");
static_assert(helper::is_type_in_namespace<::sample::True_Y>("sample::"), "2");
static_assert(helper::is_type_in_namespace<::sample::True_T<int>>("sample::"), "3");
static_assert(helper::is_type_in_namespace<::sample::True_U<int>>("sample::"), "4");
static_assert(!helper::is_type_in_namespace<::False_X>("sample::"), "5");
static_assert(!helper::is_type_in_namespace<::False_Y>("sample::"), "6");
static_assert(!helper::is_type_in_namespace<::False_T<int>>("sample::"), "7");
static_assert(!helper::is_type_in_namespace<::False_U<int>>("sample::"), "8");
}
}
}
void test4()
{
using namespace sample;
static_assert(helper::is_type_in_namespace<True_X>("sample::"), "1");
static_assert(helper::is_type_in_namespace<True_Y>("sample::"), "2");
static_assert(helper::is_type_in_namespace<True_T<int>>("sample::"), "3");
static_assert(helper::is_type_in_namespace<True_U<int>>("sample::"), "4");
}
int main(int argc, char* argv[])
{
test1();
sample::test2();
sample::inner::test3();
test4();
return 0;
}
我为MSVC2015和一些随机的在线Clang编译器以及GCC 6.1.0进行了测试。
想法:
编辑:重构代码使其更清晰并添加了GCC支持。此外,现在可以将要测试的名称空间作为参数传递
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句