以下类无法编译:
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;
};
我收到以下编译器错误:
错误:“模板 struct std::pair”的模板参数列表中的参数 2 处的类型/值不匹配
为什么下面的代码可以正常编译而无法编译?
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
不依赖于模板参数,因此在这方面没有歧义。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)