不应std :: pair< T1,T2>如果T1和T2有普通的默认构造函数? [英] shouldn't std::pair<T1,T2> have trivial default constructor if T1 and T2 have?

查看:69
本文介绍了不应std :: pair< T1,T2>如果T1和T2有普通的默认构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,因为

I ran into a problem because

 std::is_trivially_default_constructible<std::pair<T1,T2>>::value == false;

即使

 std::is_trivially_default_constructible<T1>::value == true;
 std::is_trivially_default_constructible<T2>::value == true;

我找不到这种设计的充分理由.如果T1T2有,std::pair<T1,T2>具有=default构造函数是否不合适?

I failed to find a good reason for this design. Wouldn't it appropriate for std::pair<T1,T2> to have a =default constructor if T1 and T2 have?

是否有一个简单的解决方法(比定义自己的pair<>简单)?

Is there a simple work around (simpler than defining my own pair<>)?

推荐答案

简单的原因是:历史!原始的std::pair<T0, T1>不能具有普通的默认构造函数,因为它具有其他构造函数.它被定义为初始化其成员.对于人们依赖于初始化值的琐碎可构造类型,在std::pair<T0, T1>中更改此行为将是一个重大更改.

The simple reason is: history! The original std::pair<T0, T1> couldn't have a trivial default constructor as it had some other constructor. It was defined to initialize its members. Changing this behavior in std::pair<T0, T1> for trivially constructable types where people rely on the value getting initialized would be a breaking change.

除了历史原因外,std::pair<...>的默认构造函数还定义为constexpr构造函数. constexpr默认构造函数不能为默认值.

In addition to the history reason, the default constructor of std::pair<...> is defined to be a constexpr constructor. A constexpr default constructor cannot be defaulted.

除了创建自定义类外,我不知道解决方法.

I'm not aware of a work-around other than creating a custom class.

这篇关于不应std :: pair&lt; T1,T2&gt;如果T1和T2有普通的默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆