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.
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 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).
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With