什么是 C++ 中的标量对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14821936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a scalar Object in C++?
提问by odinthenerd
As far as I understand it fundamental types are Scalar and Arrays are aggregate but what about user defined types? By what criteria would I divide them into the two categories?
据我了解,基本类型是标量,数组是聚合的,但是用户定义的类型呢?我会根据什么标准将它们分为两类?
struct S { int i; int j };
class C { public: S s1_; S s2_ };
std::vector<int> V;
std::vector<int> *pV = &v;
回答by Kerrek SB
Short version:Types in C++ are:
简短版本:C++ 中的类型是:
Object types: scalars, arrays, classes, unions
Reference types
Function types
(Member types) [see below]
void
对象类型:标量、数组、类、联合
引用类型
函数类型
(成员类型)[见下文]
void
Long version
长版
Object types
Scalars
arithmetic (integral, float)
pointers:
T *
for any typeT
enum
pointer-to-member
nullptr_t
Arrays:
T[]
orT[N]
for any complete, non-reference typeT
Classes:
class Foo
orstruct Bar
Trivial classes
Aggregates
POD classes
(etc. etc.)
Unions:
union Zip
References types:
T &
,T &&
for any object or free-function typeT
Function types
Free functions:
R foo(Arg1, Arg2, ...)
Member functions:
R T::foo(Arg1, Arg2, ...)
void
对象类型
标量
算术(整数,浮点数)
指针:
T *
对于任何类型T
枚举
指向成员的指针
nullptr_t
数组:
T[]
或T[N]
任何完整的非引用类型T
班级:
class Foo
或struct Bar
琐碎类
聚合体
POD类
(等等等等)
工会:
union Zip
引用类型:
T &
,T &&
用于任何对象或自由函数类型T
函数类型
免费功能:
R foo(Arg1, Arg2, ...)
会员功能:
R T::foo(Arg1, Arg2, ...)
void
Member types work like this. A member type is of the form T::U
, but you can't have objects or variables of member type. You can only have member pointers. A member pointer has type T::* U
, and it is a pointer-to-member-object if U
is a (free) object type, and a pointer-to-member-function if U
is a (free) function type.
成员类型的工作方式是这样的。成员类型的形式为T::U
,但您不能拥有成员类型的对象或变量。你只能有成员指针。成员指针具有 type T::* U
,如果U
是(自由)对象类型,则它是指向成员对象的指针,如果U
是(自由)函数类型,则是指向成员函数的指针。
All types are complete except void
, unsized arrays and declared-but-not-defined classes and unions. All incomplete types except void
can be completed.
除了void
、未定义大小的数组和声明但未定义的类和联合之外,所有类型都是完整的。除了void
可以完成的所有不完整类型。
All types can be const
/volatile
qualified.
所有类型都可以const
/volatile
限定。
The <type_traits>
header provides trait classes to check for each of these type characteristics.
所述<type_traits>
头部提供性状类来检查每个这些类型的特征。
回答by v.oddou
I think this would be a more comprehensive answer:
我认为这将是一个更全面的答案:
original document:
http://howardhinnant.github.io/TypeHiearchy.pdf
原始文档:http:
//howardhinnant.github.io/TypeHiearchy.pdf
a scalar is a fundamental except it cannot be void, but it can be a pointer type, or an enum type.
标量是基本的,但它不能为空,但它可以是指针类型或枚举类型。
And a fundamental has a keyword in the language. it is easy to recognize when said like that.
并且一个基本语言中有一个关键字。这样说时很容易识别。
回答by Jaege
There is a series of library classes that used for test the type of variables. std::is_scalar
can be used to test if an object is a scalar.
有一系列用于测试变量类型的库类。 std::is_scalar
可用于测试对象是否为标量。
A scalar typeis a type that has built-in functionality for the addition operator without overloads (arithmetic, pointer, member pointer, enum and
std::nullptr_t
).
甲标量类型是已经内置功能的加法运算符不重载(算术,指针,指针构件,枚举和类型
std::nullptr_t
)。
Also a table from here.
还有一张来自这里的桌子。