How to get a type alias from a template class without specifying template arguments and without instantiating it

For example I want to get the type of std::vector<>::size_type without specifying a template argument.

Here's what I want to do:

 
std::vector<>::size_type x{5};


Is there any way this can be done?
Last edited on
I don't think so. std::vector<> isn't a class (yet), it can only be compiled if it knows its type. But I don't believe you normally ever change a std::vector's size_type, so it'll always be std::size_t (see http://en.cppreference.com/w/cpp/container/vector)
So I'd just use std::size_t if I were you!

Cheers
Quote from:

http://en.cppreference.com/w/cpp/container/vector

...
size_type Unsigned integral type (usually std::size_t)
...
The reason why you cannot get type alias from templated class without specifying template parameters, is that it can differ between different types and even not exist/be not a type:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <typeinfo>

template<typename T>
struct foo
{
    using type = std::size_t; //Generic type
};

template<>
struct foo<int>
{
    using type = short; //Another type
};

template<>
struct foo<unsigned>
{
    int type; //Not a type
};

template<>
struct foo<char>
{
    //does not exist
};

int main()
{
    foo<double>::type x; //OK
    foo< int  >::type y; //OK
    std::cout << (typeid(x) == typeid(y)); // 0 — false: types of x and y are different
    //foo<unsigned>::type z; //Error: ::type is not a type.
    //foo<char>::type u; //Error: there is no member ::type
}

@BasV
@MiiNiPaa

Thank you so much for answering my question!
Topic archived. No new replies allowed.