Decltype (some_vector) :: size_type does not work as a template parameter

The following class does not compile:

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:

error: type / value mismatch in argument 2 in the list of template parameters for the template struct std :: pair


Why can't this be compiled while the following code is working 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;
};
+4
source share
1 answer

You need to tell the compiler that the dependent is size_typereally 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 independent of the template parameter, so there is no ambiguity in this regard.

+9
source

Source: https://habr.com/ru/post/1662053/


All Articles