什么是 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:43:11  来源:igfitidea点击:

What is a scalar Object in C++?

c++typestheory

提问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

      1. arithmetic (integral, float)

      2. pointers: T *for any type T

      3. enum

      4. pointer-to-member

      5. nullptr_t

    • Arrays: T[]or T[N]for any complete, non-reference type T

    • Classes: class Fooor struct Bar

      1. Trivial classes

      2. Aggregates

      3. POD classes

      4. (etc. etc.)

    • Unions: union Zip

  • References types: T &, T &&for any object or free-function type T

  • Function types

    • Free functions: R foo(Arg1, Arg2, ...)

    • Member functions: R T::foo(Arg1, Arg2, ...)

  • void

  • 对象类型

    • 标量

      1. 算术(整数,浮点数)

      2. 指针:T *对于任何类型T

      3. 枚举

      4. 指向成员的指针

      5. nullptr_t

    • 数组:T[]T[N]任何完整的非引用类型T

    • 班级:class Foostruct Bar

      1. 琐碎类

      2. 聚合体

      3. POD类

      4. (等等等等)

    • 工会: 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 Uis a (free) object type, and a pointer-to-member-function if Uis 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 voidcan be completed.

除了void、未定义大小的数组和声明但未定义的类和联合之外,所有类型都是完整的。除了void可以完成的所有不完整类型。

All types can be const/volatilequalified.

所有类型都可以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:

我认为这将是一个更全面的答案:

enter image description here

在此处输入图片说明

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_scalarcan 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.

还有一张来自这里的桌子。

C++ Type Categories

C++ 类型分类