1

I use Embacadero C++Builder. And I have the following problem:

I used to follow the guidelines and do my best not to have warnings during compilation (by fixing the code, not by disabling warnings).

Also I use std::unique_ptr<>.

But it turned out that implementation of std::unique_ptr has a bug (at least it triggers warning on 32 bit platform in release mode and when I look at implementation it seems, that the implementation is not hundred percent correct).

The specifics (example code). I try to build the following code with 32 bit compiler in release configuration

#include <memory>
using namespace std;

int main()
{
    unique_ptr<int> myUniqueInt;
    int * myInt = new int(5);
    myUniqueInt  = unique_ptr<int>(move(myInt));
    return 0;
}

Then I get the warning: [bcc32 Warning] memory(806): W8070 Function should return a value

Now if I look to the source of the warning, I see the following (file "memory", line 798, from standard library):

_Myt& operator=(_Myt&& _Right)
    {   // assign by moving _Right
    if (this != &_Right)
        {   // different, do the move
        reset(_Right.release());
        this->get_deleter() = _STD move(_Right.get_deleter());
        return (*this);
        }
    }

We can see, that indeed if this == &_Right, then function does not return any value, so the warning is right.

The questions I have are following:

  1. Why the warning is not triggered in the Debug configuration - it is only triggered in release?
  2. How can I overcome this without giving up "treat warnings as errors" globally and without disabling this warning globally.

Notes:

  • The warning presents only in Release configuration.
  • The warning present on XE6 and XE8 versions of C++Builder
  • The warning is not there for 64 bit compiler (which is expected as the compiler is different).

Thanks in advance.

Dmitrii Semikin
  • 2,134
  • 2
  • 20
  • 25
  • Note that you do not need `std::move` here. `unique_ptr(myInt);` would have been fine, `unique_ptr myUniqueInt(new int(5))` better, and `unique_ptr myUniqueInt = make_unique(5);` even better if you have make_unique. – juanchopanza May 07 '15 at 07:54
  • Thanks. Of course you are right. My original example was with two `unique_ptr` objects and there it was required. I forgot to remove it when I simplified the example. And I know about `make_unique`, but I don't have it (if I'm not mistaken, it only become available in C++14, i.e. only in latest versions of compilers). – Dmitrii Semikin May 07 '15 at 11:33
  • See [this answer](http://stackoverflow.com/a/17903225/425871) for an implementation of `make_unique()` that you can drop into your project. It's really simple. – Steve May 07 '15 at 13:40

1 Answers1

0

How can I overcome this without giving up "treat warnings as errors" globally and without disabling this warning globally.

You would have to edit <memory> itself to correct the bug. And then you should submit a bug report to the STL vendor (Dinkumware Ltd).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Well, probably, this is the most correct way to act. Though, the problem with fixing the bug in "standard" library itself is that it is not easy to distribute the fix for all the developers, which may ever work with this project... – Dmitrii Semikin May 08 '15 at 07:50