I would like to check if a type is from a particular namespace. Here is what I came up with:
#include
namespace helper
{
template
There is a (compiler specific) way to test whether a type is in a certain namespace, but I'll let you decide whether it is better than yours or not:
#include
#include
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
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
class True_T;
template
using True_U = True_T;
}
struct False_X;
class False_Y;
template
class False_T;
template
using False_U = False_T;
void test1()
{
static_assert(helper::is_type_in_namespace("sample::"), "1");
static_assert(helper::is_type_in_namespace("sample::"), "2");
static_assert(helper::is_type_in_namespace>("sample::"), "3");
static_assert(helper::is_type_in_namespace>("sample::"), "4");
static_assert(!helper::is_type_in_namespace("sample::"), "5");
static_assert(!helper::is_type_in_namespace("sample::"), "6");
static_assert(!helper::is_type_in_namespace>("sample::"), "7");
static_assert(!helper::is_type_in_namespace>("sample::"), "8");
}
namespace sample
{
void test2()
{
static_assert(helper::is_type_in_namespace("sample::"), "1");
static_assert(helper::is_type_in_namespace("sample::"), "2");
static_assert(helper::is_type_in_namespace>("sample::"), "3");
static_assert(helper::is_type_in_namespace>("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>("sample::"), "7");
static_assert(!helper::is_type_in_namespace<::False_U>("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>("sample::"), "3");
static_assert(helper::is_type_in_namespace<::sample::True_U>("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>("sample::"), "7");
static_assert(!helper::is_type_in_namespace<::False_U>("sample::"), "8");
}
}
}
void test4()
{
using namespace sample;
static_assert(helper::is_type_in_namespace("sample::"), "1");
static_assert(helper::is_type_in_namespace("sample::"), "2");
static_assert(helper::is_type_in_namespace>("sample::"), "3");
static_assert(helper::is_type_in_namespace>("sample::"), "4");
}
int main(int argc, char* argv[])
{
test1();
sample::test2();
sample::inner::test3();
test4();
return 0;
}
I tested this for MSVC2015 and some random online Clang compiler and GCC 6.1.0.
Thoughts:
Edit: Refactored code to make it more clear and added GCC support. Also, the namespace to test for can now be passed as a parameter