C++ 那些被遗漏的细节4 std::piecewise_construct_t

简述
  • 在以前的文章C++ std::pair 有对std::pair做简单介绍。
  • 需要注意的是std::pair有一个特别的构造函数,第一个参数类型为std::piecewise_construct_t, 实际就是一个空结构体类型,用作标识。
  • std::piecewise_construct_t作用就是其字面意思:分段构造。具体来说,因pair的key和value均可以是构造函数复杂类型,因而pair的初始化相对复杂,通过带有std::piecewise_construct_t类型参数,后跟两个tuple类型参数的构造函数,用第一个tuple类型的元素来构造pair的key, 用第二个tuple类型的元素来构造pair的value,从而实现pair的初始化。
  • forward_as_tuple经常与std::piecewise_construct_t配合使用
  • pair构造函数
// 头文件<utility>
pair(); (until C++11)(1)
constexpr pair(); (since C++11)(1)
(conditionally explicit)

pair( const T1& x, const T2& y );(until C++11)(2)
pair( const T1& x, const T2& y );(since C++11)(until C++14)(conditionally explicit)(2)
constexpr pair( const T1& x, const T2& y );(since C++14)(conditionally explicit)(2)
	
template< class U1, class U2 >
pair( U1&& x, U2&& y );(since C++11)(until C++14)(conditionally explicit)(3)
template< class U1, class U2 >
constexpr pair( U1&& x, U2&& y );(since C++14)(until C++23)(conditionally explicit)(3)
template< class U1 = T1, class U2 = T2 >
constexpr pair( U1&& x, U2&& y );(since C++23)(conditionally explicit)(3)

template< class U1, class U2 >
pair( const pair<U1, U2>& p );(until C++11)(4)	
template< class U1, class U2 >
pair( const pair<U1, U2>& p );(since C++11)(conditionally explicit)(4)	

template< class U1, class U2 >
pair( pair<U1, U2>&& p );(since C++11)(until C++14)(conditionally explicit)(5)	
template< class U1, class U2 >
constexpr pair( pair<U1, U2>&& p );(since C++14)(conditionally explicit)(5)	

template< class... Args1, class... Args2 >
pair( std::piecewise_construct_t,
      std::tuple<Args1...> first_args,
      std::tuple<Args2...> second_args );(since C++11)(until C++20)(6)	// 注意
template< class... Args1, class... Args2 >
constexpr pair( std::piecewise_construct_t,
                std::tuple<Args1...> first_args,
                std::tuple<Args2...> second_args );(since C++20)(6)	 // 注意
pair( const pair& p ) = default;(7)	
pair( pair&& p ) = default;(since C++11)(8)	
举例
#include <iostream>
#include <utility> // pair
#include <tuple>

struct Foo
{
    Foo(std::tuple<int, float>)
    {
        std::cout << "Constructed a Foo from a tuple\n";
    }
    Foo(int, float)
    {
        std::cout << "Constructed a Foo from an int and a float\n";
    }
};

int main()
{
    std::tuple<int, float> t(1, 3.14);
    std::pair<Foo, Foo> p1(t, t);
    std::pair<Foo, Foo> p2(std::piecewise_construct, t, t);
    return 0;
}
  • 结果
lee@leedeMacBook-Pro piecewise_construct % ./main
Constructed a Foo from a tuple
Constructed a Foo from a tuple
Constructed a Foo from an int and a float
Constructed a Foo from an int and a float
参考
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值