问题
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;
回答1:
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
arithmetic (integral, float)
pointers:
T *for any typeTenum
pointer-to-member
nullptr_t
Arrays:
T[]orT[N]for any complete, non-reference typeTClasses:
class Fooorstruct BarTrivial classes
Aggregates
POD classes
(etc. etc.)
Unions:
union Zip
References types:
T &,T &&for any object or free-function typeTFunction 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 U is 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.
回答2:
I think this would be a more comprehensive answer:
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.
回答3:
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.
A 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.
来源:https://stackoverflow.com/questions/14821936/what-is-a-scalar-object-in-c