问题描述
我知道 std :: is_pod
。但它检查不仅仅是聚合类型。或者, std :: is_pod
只是最好的我们可以做吗?
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);
只有在 T
推荐答案
无法合成 is_aggregate
模板。 C ++ 14元编程技术无法检测到某些事物是否参与聚合初始化的规则(他们需要反射支持)。
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).
没有这个的一般原因是缺乏明确的需求。即使在的情况下,应用它时仍有少许损害非聚合类型,因为统一的初始化语法可以应用于非聚合。你会使所有的转换非 - 显式
,但这是可以通过聪明的元程序/ enable_if
体操。
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.
这种事情最有用的地方是 allocator :: construct
如果 T
是聚合,则使用直接构造函数调用,否则(以避免统一初始化的不一致部分)。
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).
这篇关于如何检查T是否是聚合类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!