对和元组的piecewise_construct 的C++11 用例? [英] C++11 use-case for piecewise_construct of pair and tuple?

查看:29
本文介绍了对和元组的piecewise_construct 的C++11 用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

N3059 中,我找到了 分段构造对(和元组)(它在新标准中).

In N3059 I found the description of piecewise construction of pairs (and tuples) (and it is in the new Standard).

但是我不知道什么时候应该使用它.我发现了关于 emplace 和不可复制实体的讨论,但是当我尝试它时,我无法创建一个我需要 piecewiese_construct 或可以看到性能优势.

But I can not see when I should use it. I found discussions about emplace and non-copyable entities, but when I tried it out, I could not create a case where I need piecewiese_construct or could see a performance benefit.

示例.我以为我需要一个不可复制的类,但是movebale(转发所需):

Example. I thought I need a class which is non-copyable, but movebale (required for forwarding):

struct NoCopy {
  NoCopy(int, int) {};
  NoCopy(const NoCopy&) = delete; // no copy
  NoCopy& operator=(const NoCopy&) = delete; // no assign
  NoCopy(NoCopy&&) {}; // please move
  NoCopy& operator=(NoCopy&&) {}; // please move-assign
};

然后我有点预期标准对构造会失败:

I then sort-of expected that standard pair-construction would fail:

pair<NoCopy,NoCopy> x{ NoCopy{1,2}, NoCopy{2,3} }; // fine!

但它没有.实际上,无论如何这就是我所期望的,因为移动东西"而不是将它复制到 stdlib 中的任何地方,这是应该的.

but it did not. Actually, this is what I'd expected anyway, because "moving stuff around" rather then copying it everywhere in the stdlib, is it should be.

因此,我认为我没有理由应该这样做,或者如此:

Thus, I see no reason why I should have done this, or so:

pair<NoCopy,NoCopy> y(
    piecewise_construct,
    forward_as_tuple(1,2),
    forward_as_tuple(2,3)
); // also fine

  • 那么,什么是用例?
  • 如何以及何时使用piecewise_construct?
  • 推荐答案

    并非所有类型的移动都比复制更有效,对于某些类型,甚至显式禁用复制和移动也可能有意义.将 std::array 作为前一种类型的示例.

    Not all types can be moved more efficiently than copied, and for some types it may make sense to even explicitly disable both copying and moving. Consider std::array<int, BIGNUM> as an an example of the former kind of a type.

    emplace 函数和 piecewise_construct 的要点在于,可以就地构造这样的类,而无需创建临时实例来被移动或复制.

    The point with the emplace functions and piecewise_construct is that such a class can be constructed in place, without needing to create temporary instances to be moved or copied.

    struct big {
        int data[100];
        big(int first, int second) : data{first, second} {
            // the rest of the array is presumably filled somehow as well
        }
    };
    
    std::pair<big, big> pair(piecewise_construct, {1,2}, {3,4});
    

    将上述与 pair(big(1,2), big(3,4)) 进行比较,其中必须创建两个临时 big 对象然后复制- 移动在这里根本没有帮助!同样:

    Compare the above to pair(big(1,2), big(3,4)) where two temporary big objects would have to be created and then copied - and moving does not help here at all! Similarly:

    std::vector<big> vec;
    vec.emplace_back(1,2);
    

    分段构造对的主要用例是将元素置入 mapunordered_map:

    The main use case for piecewise constructing a pair is emplacing elements into a map or an unordered_map:

    std::map<int, big> map;
    map.emplace(std::piecewise_construct, /*key*/1, /*value*/{2,3});
    

    这篇关于对和元组的piecewise_construct 的C++11 用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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