How to check whether T is an aggregate type?

℡╲_俬逩灬. 提交于 2019-12-04 03:23:20

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/34994364/how-to-check-whether-t-is-an-aggregate-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!