English 中文(简体)
C++ - 搬迁建设与向前推进之间的相互作用
原标题:C++ - Interaction between move construction and forwarding

If I m constructing a non-trivial container like a custom map for example, and want to share code between the emplace and insert functions, one approach would be to have the insert function simply call emplace with the supplied element as the argument. eg.:

template<typename... args>
iterator emplace(int id, args &&... params)
{
    // Do complex stuff here, get a location
    *location = value_type(std::forward<args>(params) ...);
}


iterator insert(int id, const value_type &element)
{
    emplace(id, element);
}

Which in the case of insert & should simply call the copy constructor if I understand correctly? However in the case of insert && it becomes a little more confusing:

iterator insert(int id, value_type &&element)
{
    emplace(id, std::move(element));
}

<代码>std:move a. 折中元件,罚款,但将<代码>td:forward/code> 如何适当处理? e. 非属地的<代码> 数值_ 类型与非违约移动建筑商的,之上的<代码>emplace建筑线将正确地称作移动建筑商?

问题回答

在<代码>insert &中,如果我正确理解,究竟应当把复印件放在什么位置?

Yes, you re giving std::forward a const lvalue in this scenario, and std::forward yields a const lvalue. With that, the copy constructor is called.

<代码>std:move a. 折中元件,罚款,但将<代码>td:forward/code> 如何适当处理?

Yes. std::forward turns rvalues into xvalues and lvalues into lvalues. As long as you don t hand it an lvalue, it won t give you an lvalue. This subsequently means that the move constructor will be called, as intended.

In args &&... params, args deduces to value_type, making params an rvalue reference when the function template is instantiated. The second overload of std::forward is called, and std::forward<args>(params) yields an xvalue.

It s actually quite common to implement push_back or insert for containers in terms of emplace_back or emplace.





相关问题
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?

热门标签