2016-01-22 95 views
1

我希望有一个函数fn,它将指针集指向const和非const对象。我正在写一个模板来做到这一点。nullptr_type不受simple_type_specifier支持

template<typename T1, 
     typename T2, 
     std::enable_if<std::is_same<T1,NodeType *>::value && std::is_same<T2,EdgeType *>::value, std::nullptr_t>::type = nullptr> 
static void fn(unordered_set<T1> &nodeSet, unordered_set<T2>& edgeSet); 

在上述例子中,我希望能够通过unordered_set<const NodeType *>以及unordered_set<NodeType *>(simliar与EdgeType)。但是,我收到一个错误: ‘nullptr_type’ not supported by simple_type_specifier。有人可以帮忙吗?

+1

取代'的std :: nullptr_t> :: =类型nullptr'与',int> :: type = 0' – AndyG

+0

我的确想到了这一点。但是,你能告诉我为什么''nullptr''不起作用吗? – SPMP

+2

这个错误根本没有帮助,但是你只是缺少一个'typename'。 – 0x499602D2

回答

1

除了一些typename是你错过,要实现这一点,你应该使用std::remove_conststd::remove_pointer型性状:

template<typename T1, typename T2, 
    typename std::enable_if< 
    std::is_same<typename std::remove_const<typename std::remove_pointer<T1>::type>::type, NodeType>::value && 
    std::is_same<typename std::remove_const<typename std::remove_pointer<T2>::type>::type, EdgeType>::value, 
    typename std::nullptr_t>::type = nullptr> 
static void fn(std::unordered_set<T1> &nodeSet, std::unordered_set<T2>& edgeSet); 

Live Demo

+0

我最终保持它只是T1和T2,并添加一个静态断言。一种方法比另一种更好/更差吗? – SPMP

+0

另外,我在某处读取模板参数上的cv-qualifiers被忽略的地方。我显然被误解了。你能告诉我这是什么意思吗? – SPMP

+0

关于第一个问题,一切都是在编译时确定的,因此这两个解决方案中的任何一个听起来都相当于我,第二个问题:http://en.cppreference.com/w/cpp/language/template_argument_deduction – 101010