decltype(some_vector):: size_type不能用作模板参数 [英] decltype(some_vector)::size_type doesn't work as template parameter

查看:80
本文介绍了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;
};

我收到以下编译器错误:

I get the following compiler error:

错误:"template struct std :: pair"模板参数列表中参数2的类型/值不匹配

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


为什么下面的代码可以正常工作,但是为什么编译失败?


Why does that fail to compile, while the following code works fine?

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 确实是一种类型(例如,不是对象):

You need to tell the compiler that the dependent size_type is indeed a type (and not an object, for example):

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 不依赖于模板参数,因此在这方面没有歧义.

std::size_t doesn't depend on a template parameter, so there is no ambiguity in this regard.

这篇关于decltype(some_vector):: size_type不能用作模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆