Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 429
Chris Avatar asked Jan 22 '14 18:01

Chris


People also ask

How do you use sizeof in vector?

Use the vector::size() method: i < v. size() . The sizeof operator returns the size in bytes of the object or expression at compile time, which is constant for a std::vector .

How do you check the size of a vector in a vector?

To get the size of a C++ Vector, you can use size() function on the vector. size() function returns the number of elements in the vector.

How do you count the size of a vector in C++?

The built-in function exists in C++ to count the size of the vector. The function name is, size(). It returns the size or the total elements of the vector in which vector it is used. It does not take any argument.

What does sizeof vector return?

Returns the number of elements in the vector. This is the number of actual objects held in the vector, which is not necessarily equal to its storage capacity.


2 Answers

3.4.3 Qualified 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-specifier that denotes its class, namespace, or enumeration. If a :: scope resolution operator in a nested-name-specifier is not preceded by a decltype-specifier, 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.

like image 119
TemplateRex Avatar answered Sep 18 '22 14:09

TemplateRex


The member access operator . can only be used to access data members and member functions of classes, not other nested names such as type names. You'll need the scope resolution operator :: to access them, and that can only be applied to the class name (or an alias), not an object of class type:

std::vector<uint>::value_type 

In C++11 or later, decltype can give you a type name, if you have an object and no convenient access to the type:

decltype(vecs)::value_type 
like image 32
Mike Seymour Avatar answered Sep 19 '22 14:09

Mike Seymour