下面的类不编译:
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;
};
最佳答案
你需要告诉编译器依赖的 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
不依赖于模板参数,因此在这方面没有歧义。
关于c++ - decltype(some_vector)::size_type 不能用作模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40827190/