emplace functions are not implemented to 0 parameter in VS2010

Category: visual studio vclanguage

Question

AbhiMFC on Tue, 11 Jul 2017 08:38:52


Hi 

I am using VS2010 IDE, when I was trying to compile the file which is using emplace_back() function for vectors, below error is displayed. 

Error 20 error C2660: 'std::vector<_Ty>::emplace_back' : function does not take 0 argument.

But the same code is compiled in VS2012.

Please help me  if we can make it work for VS2010 ?


Replies

Guido Franzke on Tue, 11 Jul 2017 08:59:55


Hello,

show us your code if you want accurate help.

Meanwhile, I don't believe that it compiles in VS2012.

Read the documentation:

VS 2010: https://msdn.microsoft.com/en-us/library/dd647620(v=vs.100).aspx

VS 2012: https://msdn.microsoft.com/en-us/library/dd647620(v=vs.110).aspx

Regards, Guido

AbhiMFC on Tue, 11 Jul 2017 09:13:05


Hi,

Can you please try below code in both compilers, we can see the difference.

#include "stdafx.h"
#include <vector>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> myvector;
myvector.emplace_back();
return 0;
}

Pavel A on Tue, 11 Jul 2017 10:39:29


Please help me  if we can make it work for VS2010 ?

The condition after "if" is false :-(  Use a new VC++ version where this feature exists (along with new bugs...)

-- pa

Igor Tandetnik on Tue, 11 Jul 2017 11:54:28


On 7/11/2017 5:13 AM, AbhiMFC wrote:

std::vector<int> myvector;
myvector.emplace_back();

This doesn't make any sense, and correctly doesn't compile. You want to add some value to the vector - of course you need to specify that value, as an argument for emplace_back(). What exactly do you expect it to do without argument?

Baron Bi on Wed, 12 Jul 2017 09:13:15


Hi AbhiMFC,

thanks for posting here.

>>I am using VS2010 IDE, when I was trying to compile the file which is using emplace_back() function for vectors, below error is displayed. 

Error 20 error C2660: 'std::vector<_Ty>::emplace_back' : function does not take 0 argument.

But the same code is compiled in VS2012.

Please help me  if we can make it work for VS2010 ?

I'm afraid you couldn't.

In vs2010, the vector::emplace_back is implemented as below.

void emplace_back(
   Type&&_Val
);

So you need to pass an argument into it.

However since vs2012, the vector::emplace_back is implemented as below.

template <class... Types>
    void emplace_back(
        Types&&... _Args);

The emplace_back forwards its arguments to the constructor of the vector element class.

emplace_back(Object{});

So you don't have to have the Object as an argument, since it is automatically created in place for you. You only have to give the arguments for the Object-constructor to emplace_back(). If you want to append a new object with emplace_back, just call:

myvector.emplace_back();

Hope this could be help of you.

Best Regards,
Sera Yu

Guido Franzke on Wed, 12 Jul 2017 09:34:38


The emplace_back forwards its arguments to the constructor of the vector element class.

emplace_back(Object{});

So you don't have to have the Object as an argument, since it is automatically created in place for you. You only have to give the arguments for the Object-constructor to emplace_back(). If you want to append a new object with emplace_back, just call:

myvector.emplace_back();

Thanks Sera for the description. So that's why his code compiles without error.

But what value will Object{} be, if he uses type int? I follow Igor, because the code doesn't make sense (Wanting to insert a value into a vector without giving the value).

I would recommend, that AbhiMFC changes his code to insert well-defined values into a vector instead of Object{}. So why not replacing "myvector.emplace_back();" with "myvector.emplace_back(0);" ?