3

The following code doesn't compile in VS2013.

#include <memory>
#include <vector>

struct Struct {
  std::unique_ptr<int> data;
};

int main() {
  std::vector<Struct> vec;
  vec.emplace_back();
  vec.emplace_back();
  vec.front() = std::move(vec.back());
  return 0;
}

I get the following error:

error C2280: attempting to reference a deleted function

It seems like VS compiler is trying to call the assignment operator while the code explicitly requests a move. Is this a bug? Are there any workarounds for this problem?

CodeLord
  • 41
  • 4
  • I wonder why people are still bothering with VS and C++11. It's a lost cause. Always has been. – pmr Feb 28 '15 at 00:10
  • @pmr they may be locked into VS due to use of tools and components that are a part of VS – M.M Feb 28 '15 at 00:14
  • possible duplicate of [Why is this code trying to call the copy constructor?](http://stackoverflow.com/questions/8991874/why-is-this-code-trying-to-call-the-copy-constructor) –  Feb 28 '15 at 00:15
  • @remyabel that puts a bit of a damper on using Rule of Zero in this compiler – M.M Feb 28 '15 at 00:21
  • @MattMcNabb That explains why they use VS. It doesn't explain why they try to use it as a C++11 compiler which it clearly isn't. – pmr Feb 28 '15 at 00:31
  • On my VS2013 I can't even get the emplace_back to compile. – Neil Kirk Feb 28 '15 at 00:35
  • @pmr coding in C++11 is more time-efficient than using C++98 – M.M Feb 28 '15 at 00:35
  • @pmr Visual Studio's C++11 support is spotty at best (which is pants), but the tool has other advantages (excellent debugger) and why not make use of as much as it does support. – Neil Kirk Feb 28 '15 at 00:36
  • @pmr; VS is the only compiler that implements `filesystem` (no need for boost).. It also has `codecvt` which GCC/Mingw (even 4.9.2) does not have. These are some of the most wanted and awaited features. `codecvt` being the most wanted (for unicode of course). Other than that, VS is rubbish but until GCC gets those, it'll have to do. It sucks having to use third party libs. – Brandon Feb 28 '15 at 01:52

1 Answers1

3

VS2013 doesn't automatically generate the required constructors.

"Rvalue references v3.0" adds new rules to automatically generate move constructors and move assignment operators under certain conditions. However, this is not implemented in Visual C++ in Visual Studio 2013, due to time and resource constraints.

So in order to compile the program, you have to at a minimum implement these constructors:

struct Struct {
  std::unique_ptr<int> data;

  Struct() { }

  // For exposition purposes only, change as needed
  Struct(Struct&& o) : data(std::move(o.data)) {}

  Struct& operator=(Struct&& other) {
       data = std::move(other.data);
       return *this;
  }
};

It seems to be implemented in Microsoft's online compiler, though.

  • I think that online compiler might be VS2015 or one of the CTP's which seems to have implemented quite a bit. – Brandon Feb 28 '15 at 01:55
  • @Brandon The online compiler is supposedly the "latest" build, so it's going to contain all the latest features. Rextester on the other hand has Visual Studio 2013. –  Feb 28 '15 at 02:07