为什么我的标签类型不能完美转发?

时间:2015-02-26 15:08:34

标签: c++ templates

我想根据参数类型中是否存在标记类型来选择特定的类实现。如果没有完美的argumnet(这是我需要的其他原因,请参阅How to store a reference when it is an lvalue, store a copy when it is an rvalue),这样就行了。但现在它失败了。为什么,以及如何修复它?

#include <utility>

template< typename T > 
struct always_false : std::false_type {};

struct gpio { typedef void is_gpio; };

template <int N >
struct lpc_gpio : public gpio {};

template< typename T, typename dummy = void>
struct invert_impl {
   static_assert( always_false< T >::value, "cannot invert this type" );  
};

template< typename T >
struct invert_impl< T, typename T::is_gpio > : public gpio {};

template< typename P >
invert_impl< P > invert( P && pin ){
    return invert_impl< P >( pin );
}

int main(){
   auto pin4 = lpc_gpio< 4 >{};
   // the next line rpoduces the static_assert
   auto led = invert( pin4 );
}

我的预感是我应该删除引用,typename remove_reference_t&lt; P&gt; :: is_gpio(或者更确切地说,11当量),但这不起作用:

  #include <utility>
  #include <type_traits>

  template< typename T > 
  struct always_false : std::false_type {};

  struct gpio { typedef void is_gpio; };

  template <int N >
  struct lpc_gpio : public gpio { };

  template< typename T, typename dummy = void>
  struct invert_impl {
     static_assert( always_false< T >::value, "cannot invert this type" );  
  };

  template< typename T >
  struct invert_impl< T, typename std::remove_reference< T >::type::is_gpio > : public gpio {};

  template< typename P >
  invert_impl< P > invert( P && pin ){
      return invert_impl< P >( pin ); // line 22
  }

  int main(){
     auto pin4 = lpc_gpio< 4 >{};
     // the next line rpoduces the static_assert
     auto led = invert( pin4 ); // line 28
     (void)led;
  }

给出

main.cpp: In instantiation of 'invert_impl<P> invert(P&&) [with P = lpc_gpio<4>&]':
main.cpp:28:34:   required from here
main.cpp:22:40: error: no matching function for call to 'invert_impl<lpc_gpio<4>&, void>::invert_impl(lpc_gpio<4>&)'
       return invert_impl< P >( pin );    

0 个答案:

没有答案