• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 55
  • Last Modified:

std::unique_ptr Problems with Visual Studio 2010

I try to compile a code that was successfully compile on Linux system.  This code hast the usage of std::unique_ptr but Visual Studio nags arround the common C++ usage. The code looks like

    std::unique_ptr<bxf2::bxf> document = nullptr;
    ofstream cerr("output.txt");
    
    try
    {
        xml_schema::properties props;
        document = move(bxf2::bxf_(bxf_file, 0, props));  (Line 29)
    }

Open in new window


THe error message is:

1>XSDValidator.cxx(29): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::auto_ptr<_Ty>' (or there is no acceptable conversion)
1>          with
1>          [
1>              _Ty=bxf2::bxf
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\memory(2420): could be 'std::unique_ptr<_Ty> &std::unique_ptr<_Ty>::operator =(std::unique_ptr<_Ty> &&)'
1>          with
1>          [
1>              _Ty=bxf2::bxf []
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\memory(2449): or       'std::unique_ptr<_Ty> &std::unique_ptr<_Ty>::operator =(std::nullptr_t)'
1>          with
1>          [
1>              _Ty=bxf2::bxf []
1>          ]
1>          C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\memory(2520): or       'std::unique_ptr<_Ty> &std::unique_ptr<_Ty>::operator =(const std::unique_ptr<_Ty> &)'
1>          with
1>          [
1>              _Ty=bxf2::bxf []
1>          ]
1>          while trying to match the argument list '(std::unique_ptr<_Ty>, std::auto_ptr<_Ty>)'
1>          with
1>          [
1>              _Ty=bxf2::bxf []
1>          ]
1>          and
1>          [
1>              _Ty=bxf2::bxf
1>          ]

Open in new window


What I have done wrong in Visual Studio? IS Visual Studio the wrong version to use?
0
Ingo Foerster
Asked:
Ingo Foerster
  • 2
2 Solutions
 
evilrixSenior Software Engineer (Avast)Commented:
Try changing

document = move(bxf2::bxf_(bxf_file, 0, props));

to

document = bxf2::bxf_(bxf_file, 0, props);

Since the return value is an r-value already, the move shouldn't be needed.

See example, below - this works fine for me with 2017.

#include <memory>

struct foo{};

std::unique_ptr<foo> bar()
{
   auto pFoo = std::unique_ptr<foo>();
   return pFoo;
}

int main()
{

   std::unique_ptr<foo> pFoo = nullptr;
   pFoo = bar();
}

Open in new window

0
 
sarabandeCommented:
std::unique_ptr<bxf2::bxf> document = nullptr

you were using type 'bxf2::bxf' here and  'bxf2::bxf_' in the move. is that correct?

you could try

document = (std::unique_ptr<bxf2::bxf> &) move(bxf2::bxf_(bxf_file, 0, props));

Open in new window


to solve the ambuigity with a cast. but that shouldn't be necessary.

note visual studio 2010 was released before C++11. what means that the compiler might have some lacks regarding c++11.

Sara
0
 
sarabandeCommented:
There was no feedback from the Author regarding the suggestions made. But both the comments are likely to solve the issue and are valid for PAQ.

Sara
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now