error C4146: unary minus operator applied to unsigned type, result still unsigned
Asked Answered
B

5

5

I try to build CRF++ in Visual Studio 2013 and get this error in the last line:

array_[begin + siblings[i].code].base =
            value_ ?
            static_cast<array_type_>(-value_[siblings[i].left]-1) :
            static_cast<array_type_>(-siblings[i].left-1);

error C4146: unary minus operator applied to unsigned type, result still unsigned

Specifically, it is in darts.h, line 189.

I built again in Visual Studio 2015 then there is no error.

How can I fix this in Visual Studio 2013?

Bergquist answered 1/4, 2016 at 3:54 Comment(5)
What is the type of value_?Dysthymia
@HappyCoder: array_type_ *value_;Bergquist
And array_type_ is defined as?Dysthymia
Why static_cast<array_type_> something if is already array_type_ once dereferenced via value_[...], where you've said above value_ is array_type_* ?Lepanto
@HappyCoder: It is a template type which can be short, int or long.Bergquist
A
2

Try this:

int tmp = static_cast<int>(siblings[i].left);

array_[begin + siblings[i].code].base =
            value_ ?
            static_cast<array_type_>(-value_[siblings[i].left]-1) :
            static_cast<array_type_>(-tmp - 1);
Andreeandrei answered 11/7, 2016 at 4:8 Comment(0)
A
9

C4146 is not supposed to be an error. It is a mere warning. If you see it as an error in your case, it means that someone has configured it this way, most likely unintentionally. This makes no sense.

Find and undo the changes that turned it into an error. Then you can disable it if you wish.

Accelerate answered 1/4, 2016 at 4:8 Comment(3)
Thanks, AnT. I get this information from Error list window. I installed Visual Studio 2013 by default and have not changed anything in settings & properties of my project. I read a few post about "Treat warnings as errors" but it didn't work.Bergquist
It appears that one version of this problem is a long-standing bug in Visual Studio. It is supposedly fixed in the (upcoming, as of this writing) version 16.4 preview 1.Mindful
They say it's been fixed but here I'm using VS 2019 and getting the same errorBarkley
K
9

For others I wanted to add another answer in case they stumble into that bug like me.

Even if "Threat warning as error" was off in my compiler, I had to compile my project with #pragma warning(disable:4146) in my header's file that was showing me the error. For you it would had been inside darts.h

Please note disabling the warning globally in my project was not working (compiler /wd4146), the pragma line was needed in the header's file directly.

Kipton answered 8/9, 2016 at 1:12 Comment(0)
E
4

the siblings variable also has unary minus applied to it, maybe that is the culprit? Also if it's templated code you cannot really be sure value_ is going to be be short, int or long unless that is asserted via static_assert or so. We can only guess as you don't provide information about what types are actually used or something reproducable. Also did you use the exact same project for compilation in VS2015? If not, the warning might just have been disabled there.

Anyway, suppose it is a bug in VS2013, it likely won't ever get fixed anymore so you can try to find a workaround. Start by breaking that statement down into smaller ones until you know exactly which part is the problem (or maybe, by then the problem is gone already as it goes with compiler bugs). Then suppress the warning with #pragma warning ( disable : 4146 ), wrapped in a conditional directive so it only has effect for VS2013 #if _MSC_VER > 1800 && _MSC_VER < 1900 should do fine. Enable the warning again after the statement. Add a comment as to why the warning is disabled and submit the changes as a patch to CRF++.

Expedite answered 1/4, 2016 at 6:41 Comment(0)
J
3

For anyone running into the really confusing part of this:

This isn't supposed to be an error. In fact, there are a bunch of warnings that Visual Studio incorrectly turns into errors by default. Compilers can add warnings for whatever they want, but throwing errors for valid code is just a bug.

The culprit is the "SDL checks" option. It's known for adding things like runtime stack checking, which is fine, but they snuck a bit of nastiness in: "and enables extra security-relevant warnings as errors". That needs to be turned off to have something slightly closer to a standard C++ compiler.

Jeavons answered 18/9, 2022 at 17:45 Comment(0)
A
2

Try this:

int tmp = static_cast<int>(siblings[i].left);

array_[begin + siblings[i].code].base =
            value_ ?
            static_cast<array_type_>(-value_[siblings[i].left]-1) :
            static_cast<array_type_>(-tmp - 1);
Andreeandrei answered 11/7, 2016 at 4:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.