c++ scalar 标量类型

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;
share improve this question
 
 
I think you may want to clean up the spelling and syntax of your introductory paragraph, and possibly expanind on it a bit if you want useful answers –  tletnes  Feb 11 '13 at 22:19
 
Do you mean "types" and "aggregate"? –  us2012  Feb 11 '13 at 22:19
2 
@us2012: "Tapes and Arrogates" should be an indie college band. –  Kerrek SB  Feb 11 '13 at 22:36

3 Answers

up vote 24 down vote accepted

Short version: Types in C++ are:

  • Object types: scalars, arrays, classes, unions

  • Reference types

  • Function types

  • (Member types) [see below]

  • 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 Foo or 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

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 Uis a (free) function type.

All types are complete except void, unsized arrays and declared-but-not-defined classes and unions. All incomplete types except void can be completed.

All types can be const/volatile qualified.

The <type_traits> header provides trait classes to check for each of these type characteristics.

share improve this answer
 

I think this would be a more comprehensive answer:

enter image description here

original document:
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.

share improve this answer
 

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.

scalar type is a type that has built-in functionality for the addition operator without overloads (arithmetic, pointer, member pointer, enum and std::nullptr_t).

Also a table from here.

C++ Type Categories

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值