[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

std::unique_ptr Problems with Visual Studio 2010

Posted on 2017-09-19
3
Low Priority
?
34 Views
Last Modified: 2017-10-09
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
Comment
Question by:Ingo Foerster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points (awarded by participants)
ID: 42300771
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
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 500 total points (awarded by participants)
ID: 42301247
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
 
LVL 35

Expert Comment

by:sarabande
ID: 42323763
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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question