Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

c++ - How can I get sizeof a vector::value_type?

I want to get sizeof of the type that is contained in a vector. Here is what I tried:

#include <iostream>
#include <vector>

int main()
{
    std::vector<uint> vecs;
    std::cout << sizeof(vecs.value_type) << std::endl;
    return 0;
}

From my understanding this should be correct. However, when compiling with GCC 4.8.1 this is what I get:

test-sizeof.cpp: In function ‘int main()’:
test-sizeof.cpp:7:27: error: invalid use of ‘std::vector<unsigned int>::value_type’
  std::cout << sizeof(vecs.value_type) << std::endl;
                           ^

What am I doing wrong? How can I get the size of the contained type?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

3.4.3 Quali?ed name lookup [basic.lookup.qual]

1 The name of a class or namespace member or enumerator can be referred to after the :: scope resolution operator (5.1) applied to a nested-name-speci?er that denotes its class, namespace, or enumeration. If a :: scope resolution operator in a nested-name-speci?er is not preceded by a decltype-speci?er, lookup of the name preceding that :: considers only namespaces, types, and templates whose specializations are types. If the name found does not designate a namespace or a class, enumeration, or dependent type, the program is ill-formed.

In this case, you are accessing a type member from the class template specialization std::vector<uint>, and you need to do it by writing:

std::vector<uint>::value_type

In case you are actually inside templated code and want to e.g. access the same nested type, you need to prefix it with the keyword typename like this:

typename std::vector<T>::value_type

In C++11, you can use sizeof(decltype(vecs)::value_type) or also sizeof(decltype(vecs.back())), the latter is convenient if you don't know the precise name of the type but know how to access them through a member function like back().

Note: as pointed out by @Casey in the comments, decltype requires stripping references in order to get the type itself, but for sizeof purposes that doesn't matter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...