English 中文(简体)
st : set :如果存在,则会提出争论
原标题:std::unordered_set<T>::insert(T&&): is argument moved if it exists
  • 时间:2012-04-06 12:49:32
  •  标签:
  • c++
  • c++11

This question is about the specification of several functions in the C++11 Standard Library, that take their arguments as rvalue references, but do not consume them in all cases. One example is std::unordered_set<T>::insert(T&&).

It is pretty clear, that this method will use the move constructor of T to construct the element within the container, if it does not already exist. However, what happens if the element already exists in the container? I am pretty sure there is no reason the change the object in the case. However, I didn t find anything in the C++11 Standard supporting my claim.

这里是一个例子,可以说明为什么这一点可能令人感兴趣。 下面的法典中行文如下:混淆并删除重复线的首次出现。

std::unordered_set<std::string> seen;
std::string line;
while (getline(std::cin, line)) {
    bool inserted = seen.insert(std::move(line)).second;
    if (!inserted) {
        /* Is it safe to use line here, i.e. can I assume that the
         * insert operation hasn t changed the string object, because 
         * the string already exists, so there is no need to consume it. */
        std::cout << line <<  
 ;
    }
}

Apparently, this example works with GCC 4.7. But I am not sure, if it is correct according to the standard.

最佳回答

我在标准(17.4.6.9)中看到这一说明:

[<>说明> 如果一项方案在将该升值转换为图书馆功能时将升值为微值(例如,通过将这一功能称作move(x)),则该方案实际上要求该功能将这一升值作为临时处理。 执行是免费的,可以优化检查,如果这一论点具有价值,则可能需要检查。 - 最后说明

虽然它没有直接回答你的问题,但它确实表明,你在图书馆职能上有效地“证明”这一论点是临时性的,因此,一旦你叫<条码><<<>sert>/code>,我就不依赖其价值。 就我所知,即使图书馆随后确定集装箱内的价值没有固定,其实施也有权从参数上移走。

问题回答

对第23.2.5/表103中未定的关联集装箱的<代码>insert的斜体没有具体说明,如果插入无效,该词的动构造是否可援引insert/code>。

<><>>>a_uniq.insert(t)

回归:pair<iterator, bool>

Requires: If t is a non-const rvalue expression, T shall be MoveInsertable into X; otherwise, T shall be CopyInsertable into X.

Effects: Inserts t if and only if there is no element in the container with key equivalent to the key of t. The bool component of the returned pair indicates whether the insertion takes place, and the iterator component points to the element with key equivalent to the key of t.

However, the specification for emplace is clearer (also from Table 103), and you should be able to use it instead of insert to get the guarantees you want:

<>条码>a_uniq.emplace(args)

回归:pair<iterator, bool>

Requires: T shall be EmplaceConstructible into X from args.

Effects: Inserts a T object t constructed with std::forward(args)... if and only if there is no element in the container with key equivalent to the key of t. The bool component of the returned pair is true if and only if the insertion takes place, and the iterator component of the pair points to the element with key equivalent to the key of t.

I interpret this to mean ((Inserts a Tbjectt) 构造......(如果并且只在集装箱内没有元素......)i.e.,只有在集装箱内没有配对元素的情况下,才应当插入和建造。 如无物体构造,std:string, 您的通行证将永远不会传给移动建筑商,因此,在emplace打电话后仍然有效。

g cc 4.7.0吨似乎支持<代码>unordered_set:emplace,但符合标准(23.5.6.1)

正如@NicolBolas在评论中指出的,尽管有上述意见,但如果存在相互冲突的条目,则不可能执行<代码>emplace的功能。

因此,以符合标准的方式获得您所希望的语种的唯一途径是填写find。 之后,有条件地采用<代码>sert或emplace

这是正确的。

当然,在处理哲学问题时,任何东西都可以受到质疑,但汇编者必须做些什么。

The choice of the designers was that -to perform a move- there must be a place to go. If there is no such a place, the move doesn t happen.

Note that whatever function takes a &&, it assume the the parameter is "temporary": if it doesn t "stole" the data, the temporary will be destroyed at the expression end. If the temporariness had been coerced (via std::move), the object will stay in any case there until destroyed by its own scope. With or without its original data inside.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签