2016-01-22 100 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