如何获取sizeof一个向量:: value_type? [英] How can I get sizeof a vector::value_type?
问题描述
我想得到 sizeof
包含在向量中的类型。这里是我尝试:
#include< iostream&
#include< vector>
int main()
{
std :: vector< uint& vecs;
std :: cout<< sizeof(vecs.value_type)<< std :: endl;
return 0;
}
根据我的理解,这应该是正确的。然而,当使用GCC 4.8.1编译时,这是我得到的:
test-sizeof.cpp:在函数'int main()':
test-sizeof.cpp:7:27:error:invalid使用'std :: vector< unsigned int> :: value_type'
std :: cout< sizeof(vecs.value_type)<< std :: endl;
^
我做错了什么?如何获得所包含类型的大小?
3.4.3限定名称查找[basic.lookup。 qual]
1类或命名空间成员或枚举器的名称可以是
, :范围解析运算符(5.1),应用于表示其类,命名空间或
枚举的
嵌套名称规范。如果
嵌套名称规范中的:: scope解析操作符前面没有decltype-specifier,则查找
前面的名称只考虑命名空间,类型和
专业类型为的模板。如果找到的名称不是
,则指定一个命名空间或类,枚举或依赖类型,
程序不成形。
在这种情况下,您正在从类模板专业化中访问类型
成员 std :: vector< uint>
,你需要这样写:
std :: vector< uint> :: value_type
如果你实际上是模板化的代码,访问相同的嵌套类型,您需要使用关键字 typename
为其前缀,如下所示:
typename std :: vector< T> :: value_type
,您可以使用 sizeof(decltype(vecs):: value_type)
或也可以使用 sizeof(decltype(vecs.back()))
,如果你不知道类型的精确名称,但是知道如何通过 back()
的成员函数访问它们, p>
注意:如@Casey在注释中指出, decltype
为了获得类型本身,但是为了大小的目的,无关紧要。
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?
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.
这篇关于如何获取sizeof一个向量:: value_type?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!