views:

285

answers:

3

I recently installed Visual Studio 2010 Professional RC to try it out and test the few C++0x features that are implemented in VC++ 2010.

I instantiated a std::vector of std::unique_ptr, without any problems. However, when I try to populate it by passing temporaries to push_back, the compiler complains that the copy constructor of unique_ptr is private. I tried inserting an lvalue by moving it, and it works just fine.

#include <utility>
#include <vector>

int main()
{
    typedef std::unique_ptr<int> int_ptr;

    int_ptr pi(new int(1));

    std::vector<int_ptr> vec;

    vec.push_back(std::move(pi));      // OK
    vec.push_back(int_ptr(new int(2))); // compiler error
}

As it turns out, the problem is neither unique_ptr nor vector::push_back but the way VC++ resolves overloads when dealing with rvalues, as demonstrated by the following code:

struct MoveOnly
{
    MoveOnly() {}
    MoveOnly(MoveOnly && other) {}

private:

    MoveOnly(const MoveOnly & other);
};

void acceptRValue(MoveOnly && mo) {}

int main()
{
    acceptRValue(MoveOnly()); // Compiler error
}

The compiler complains that the copy constructor is not accessible. If I make it public, the program compiles (even though the copy constructor is not defined).

Did I misunderstand something about rvalue references, or is it a (possibly known) bug in VC++ 2010 implementation of this feature?

+2  A: 

First of all, you need a close ):

vec.push_back(int_ptr(new int(2))); // compiler error

Now I have no compiler error neither the first nor the second case.

I use Visual Studio 2010 Beta.

Alexey Malistov
Sorry, I must have accidentally erased the parenthesis when writing my question. Thanks for testing this in the Beta, it strengthens my feeling that this is a bug in the RC...
Luc Touraille
Works fine for me on VS2010 RC.
Sumant
Hum, this is getting weirder and weirder. Could you provide the exact version of Visual C++ 2010 you have installed?
Luc Touraille
Microsoft Visual Studio 2010 10.0.30128.1 RC1Rel
Sumant
I have the same version, it turns out the problem was that I had disabled language extensions.
Luc Touraille
A: 

I noticed that I had disabled language extensions (\Za). With the extensions enabled, the code gets correctly compiled. I still think this is a bug since the code presented here is perfectly standard (as far as I know) and does not rely on any Microsoft extensions.

Luc Touraille
+4  A: 

Unfortunately, /Za is buggy. It performs an elided-copy-constructor-accessibility check when it shouldn't (binding rvalue references doesn't invoke copy constructors, even theoretically). As a result, /Za should not be used.

Stephan T. Lavavej, Visual C++ Libraries Developer ([email protected])

Stephan T. Lavavej
Thanks for the confirmation!
Luc Touraille