检查类型是否来自特定的命名空间 [英] Check if a type is from a particular namespace

查看:91
本文介绍了检查类型是否来自特定的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查类型是否来自特定的命名空间。这是我想出的:

I would like to check if a type is from a particular namespace. Here is what I came up with:

#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 添加到自己的命名空间(甚至是全局命名空间)。当然,我将为每个我想测试的命名空间创建一个这样的结构。

This works fine as long as no one adds adl_is_member_of_sample to their own namespace (or even the global namespace). And of course, I would have to create such a construct for each namespace I want to test for.

有一个更好的方法来检查在编译时如果一个类型是从特定的命名空间?

Is there a better way to check at compile time if a type is from a particular namespace?



理由或我为什么要这样:

在EDSL中,我在编译时检查类型traits以查看某些表达式是否有效。其中一些类型traits是相当简单的:如果一个类有一个使用is_numeric = void ,那么我把它作为一个数字表达式。工作正常。

In an EDSL, I am checking type traits at compile time to see whether certain expressions are valid or not. Some of those type traits are fairly simple: If a class has a using is_numeric = void, then I treat it as a numeric expression. Works fine.

is_numeric 其他人也可以使用它。

is_numeric is pretty generic though. Others might use it as well. I therefore thought about backing the trait with a check that the type is from the expected namespace.

推荐答案

这是我如何看到一个可能的解决方案。您可能需要添加 #include< type_traits>

This is how I see a possible solution. You might need to add #include <type_traits>

数字命名空间示例

namespace numeric{
    typedef double number;
    class A{};

    template<class T>
    struct is_from_numeric : integral_constant<
       bool,
       is_same<number, T>::value ||
       is_same<A, T>::value
    >{};
};

非数值命名空间示例

namespace nonNumeric{
    typedef char character;
    class A{};

    template<class T>
    struct is_from_nonNumeric : integral_constant<
       bool,
       is_same<character, T>::value ||
       is_same<A, T>::value
    >{};
};

仅用于测试目的,定义不属于任一命名空间的类

For testing purpose only, define the class which doesn't belong to either of namespaces

class B{};

为方便起见,写入单独的模板函数,回答问题:是类型属于数字/非数字命名空间?

For convenience write separate template functions which answers the question: is this type belong to numeric/non-numeric namespace?

template<class T> bool isFromNumeric(T &dummy){
   return numeric::is_from_numeric<T>::value; 
}
template<class T> bool isFromNonNumeric(T &dummy){
   return nonNumeric::is_from_nonNumeric<T>::value; 
}

你可以写出决定的函数,

after you have it, you can write the function which make decision or simply answer the question


$ b如果某个类型来自特定的命名空间,则

if a type is from a particular namespace


$ b

template<class T>
bool fromWhereIs(T &v){
   if(isFromNumeric(v)){
      cout << "# it is form \"numeric\" namespace" << endl;
      return true;
   }

   if(isFromNonNumeric(v))
      cout << "# it is from \"nonNumeric\" namespace" << endl;
   else
      cout << "# \"unknown\" namespace " << endl;

   return false;
} 

使用示例

using namespace numeric;
//using namespace nonNumeric; // Test with it later

int main(){
   cout << boolalpha;

   number value;
   double b2;
   A test1;
   nonNumeric::A test2;
   B test3;

   cout << isFromNumeric(test1) << endl;
   fromWhereIs(test1);
   fromWhereIs(test2);
   fromWhereIs(test3);
   fromWhereIs(value);
   fromWhereIs(b2);
   return 0;
}

这篇关于检查类型是否来自特定的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆