Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether T is an aggregate type?

I know about std::is_pod. But it checks more than just aggregate types. Or, is std::is_pod just the best we can do?

Basically, I want to write a function template for this:

template <typename T>
aggregate_wrapper<T> wrap(T&& x);

which is only enabled when T is an aggregate type.

like image 910
Lingxi Avatar asked Jan 25 '16 13:01

Lingxi


People also ask

What is an aggregate type?

An aggregate type is a structure, union, or array type. If an aggregate type contains members of aggregate types, the initialization rules apply recursively.

What are aggregate types in C++?

What are aggregates and why they are special. Formal definition from the C++ standard (C++03 8.5. 1 §1): An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).

Is a struct an aggregate?

A struct is an aggregate data type consisting of one or more other types. struct IntList { int value; struct IntList *next; }; This struct contains an integer and a pointer. value and next are called members of the structure.


1 Answers

There is no way to synthesize an is_aggregate template. The rules for whether something participates in aggregate initialization cannot be detected by C++14 metaprogramming techniques (they would require reflection support).

The general reason for not having this is the lack of an explicit need. Even in the case of your wrapper, there's little harm in applying it to non-aggregate types, since uniform initialization syntax can be applied to non-aggregates. You'll make all conversions non-explicit, but that's something which can be fixed via clever metaprogramming/enable_if gymnastics.

The most useful place for such a thing would be in allocator::construct, which would allow you to use aggregate initialization to construct the object if T were an aggregate, while using direct constructor calls otherwise (to dodge the "not uniform" part of uniform initialization).

like image 84
Nicol Bolas Avatar answered Nov 14 '22 21:11

Nicol Bolas