2016-11-27 95 views
4

下面的類不會編譯:decltype(some_vector):: SIZE_TYPE不作爲模板參數工作

template<class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key>> 
class MyContainer 
{ 
public: 
    std::vector<Key, Allocator> data; 
    std::vector<std::pair<std::size_t, decltype(data)::size_type>> order; 
}; 

我得到以下編譯器錯誤:

error: type/value mismatch at argument 2 in template parameter list for ‘template struct std::pair’


爲什麼說無法編譯,而下面的代碼工作正常?

template<class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key>> 
class MyContainer 
{ 
public: 
    std::vector<Key, Allocator> data; 
    std::vector<std::pair<std::size_t, std::size_t>> order; 
}; 

回答

9

你需要告訴編譯器的依賴size_type確實是一個類型(而不是對象,例如):

template<class Key, class Compare = std::less<Key>, class Allocator = std::allocator<Key>> 
class MyContainer 
{ 
public: 
    std::vector<Key, Allocator> data; 
    std::vector<std::pair<std::size_t, typename decltype(data)::size_type>> order; 
             ^^^^^^^^ 
}; 

std::size_t不依賴模板參數,所以有在這方面不含糊。

+2

只是完全醜陋! (但它有效......:/) –

+0

「而不是一個對象」,這碰到了頭部的指甲。 – Surt