在编译时检测typedef(模板元编程) [英] detecting typedef at compile time (template metaprogramming)

查看:117
本文介绍了在编译时检测typedef(模板元编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在做一些模板元编程。在我的情况下,我可以处理任何可迭代类型,即任何类型的 typedef foo const_iterator 以相同的方式存在。我试图使用新的C ++ 11模板元编程为此,但是我找不到一个方法来检测是否缺少某种类型。



因为我也需要打开/关闭基于其他特性的其他模板专门化,我目前使用带有两个参数的模板,第二个通过 std :: enable_if 生成。这是我目前正在做的:

  template< typename T,typename Enable = void> 
struct Foo {}; // default case is invalid

template< typename T>
struct Foo< T,typename std :: enable_if< std :: is_fundamental< T> :: value> :: type> {
void do_stuff(){...}
}

template< typename T>
struct exists {
static const bool value = true;
};

template< typename T>
struct Foo< T,typename std :: enable_if< exists< typename T :: const_iterator> :: value> :: type> {
void do_stuff(){...}
};

我没有 exists 帮助模板。例如,简单地执行

  template< typename T> 
struct Foo< T,typename T :: const_iterator> {
void do_stuff(){...}
};

没有工作,因为在应该使用这种特殊化的情况下,无效的默认情况被实例化



但是我在新的C ++ 11标准中找不到 exists 因为我知道只是从 boost :: type_traits 这种东西。但是,在首页提升:: type_traits 不会显示任何可以代替的参考。



这个功能缺失,如果你只是想要一个给定的类型包含 const_iterator

code>,然后是您的代码的简化版本:

  template< typename T& 
struct void_ {typedef void type; };

template< typename T,typename = void>
struct Foo {};

template< typename T>
struct Foo< T,typename void_< typename T :: const_iterator> :: type> {
void do_stuff(){...}
};


I am currently doing some template metaprogramming. In my case I can handle any "iteratable" type, i.e. any type for which a typedef foo const_iterator exists in the same manner. I was trying to use the new C++11 template metaprogramming for this, however I could not find a method to detect if a certain type is missing.

Because I also need to turn on/off other template specializations based on other characteristics, I am currently using a template with two parameters, and the second one gets produced via std::enable_if. Here is what I am currently doing:

template <typename T, typename Enable = void>
struct Foo{}; // default case is invalid

template <typename T>
struct Foo< T, typename std::enable_if<std::is_fundamental<T>::value>::type>{ 
   void do_stuff(){ ... }
};

template<typename T>
struct exists{
   static const bool value = true;
};

template<typename T>
struct Foo<T, typename std::enable_if<exists< typename T::const_iterator >::value >::type> {
    void do_stuff(){ ... }
};

I was not able to do something like this without the exists helper template. For example simply doing

template<typename T>
struct Foo<T, typename T::const_iterator> {
    void do_stuff(){ ... }
};

did not work, because in those cases where this specialization should be used, the invalid default case was instantiated instead.

However I could not find this exists anywhere in the new C++11 standard, which as far as I know simply is taking from boost::type_traits for this kind of stuff. However on the homepage for boost::type_traits does not show any reference to anything that could be used instead.

Is this functionality missing, or did I overlook some other obvious way to achieve the desired behavior?

解决方案

If you simply want if a given type contains const_iterator then following is a simplified version of your code:

template<typename T>
struct void_ { typedef void type; };

template<typename T, typename = void>
struct Foo {};

template<typename T>
struct Foo <T, typename void_<typename T::const_iterator>::type> {
      void do_stuff(){ ... }
};

这篇关于在编译时检测typedef(模板元编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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