cxxopts icon indicating copy to clipboard operation
cxxopts copied to clipboard

compile error with -Wconversion

Open maidamai0 opened this issue 5 years ago • 8 comments

Got compile error with flag -Wall -Werror -Wconversion

here is the output of compiler:

In file included from /home/thy/Downloads/cxxopts/src/example.cpp:27:0:
/home/thy/Downloads/cxxopts/include/cxxopts.hpp: In instantiation of ‘void cxxopts::values::integer_parser(const string&, T&) [with T = unsigned char; std::__cxx11::string = std::__cxx11::basic_string<char>]’:
/home/thy/Downloads/cxxopts/include/cxxopts.hpp:622:33:   required from here
/home/thy/Downloads/cxxopts/include/cxxopts.hpp:591:32: error: conversion to ‘US {aka unsigned char}’ from ‘int’ may alter its value [-Werror=conversion]
         result = result * base + digit;
                  ~~~~~~~~~~~~~~^~~~~~~
/home/thy/Downloads/cxxopts/include/cxxopts.hpp: In instantiation of ‘void cxxopts::values::integer_parser(const string&, T&) [with T = signed char; std::__cxx11::string = std::__cxx11::basic_string<char>]’:
/home/thy/Downloads/cxxopts/include/cxxopts.hpp:629:33:   required from here
/home/thy/Downloads/cxxopts/include/cxxopts.hpp:591:32: error: conversion to ‘US {aka unsigned char}’ from ‘int’ may alter its value [-Werror=conversion]
/home/thy/Downloads/cxxopts/include/cxxopts.hpp: In instantiation of ‘void cxxopts::values::integer_parser(const string&, T&) [with T = short unsigned int; std::__cxx11::string = std::__cxx11::basic_string<char>]’:
/home/thy/Downloads/cxxopts/include/cxxopts.hpp:636:33:   required from here
/home/thy/Downloads/cxxopts/include/cxxopts.hpp:591:32: error: conversion to ‘US {aka short unsigned int}’ from ‘int’ may alter its value [-Werror=conversion]
/home/thy/Downloads/cxxopts/include/cxxopts.hpp: In instantiation of ‘void cxxopts::values::integer_parser(const string&, T&) [with T = short int; std::__cxx11::string = std::__cxx11::basic_string<char>]’:
/home/thy/Downloads/cxxopts/include/cxxopts.hpp:643:33:   required from here
/home/thy/Downloads/cxxopts/include/cxxopts.hpp:591:32: error: conversion to ‘US {aka short unsigned int}’ from ‘int’ may alter its value [-Werror=conversion]
/home/thy/Downloads/cxxopts/include/cxxopts.hpp: In instantiation of ‘R cxxopts::values::checked_negate(T&&, const string&, std::true_type) [with R = signed char; T = unsigned char&; std::__cxx11::string = std::__cxx11::basic_string<char>; std::true_type = std::integral_constant<bool, true>]’:
/home/thy/Downloads/cxxopts/include/cxxopts.hpp:598:34:   required from ‘void cxxopts::values::integer_parser(const string&, T&) [with T = signed char; std::__cxx11::string = std::__cxx11::basic_string<char>]’
/home/thy/Downloads/cxxopts/include/cxxopts.hpp:629:33:   required from here
/home/thy/Downloads/cxxopts/include/cxxopts.hpp:526:31: error: conversion to ‘signed char’ from ‘int’ may alter its value [-Werror=conversion]
       return -static_cast<R>(t);
                               ^
/home/thy/Downloads/cxxopts/include/cxxopts.hpp: In instantiation of ‘R cxxopts::values::checked_negate(T&&, const string&, std::true_type) [with R = short int; T = short unsigned int&; std::__cxx11::string = std::__cxx11::basic_string<char>; std::true_type = std::integral_constant<bool, true>]’:
/home/thy/Downloads/cxxopts/include/cxxopts.hpp:598:34:   required from ‘void cxxopts::values::integer_parser(const string&, T&) [with T = short int; std::__cxx11::string = std::__cxx11::basic_string<char>]’
/home/thy/Downloads/cxxopts/include/cxxopts.hpp:643:33:   required from here
/home/thy/Downloads/cxxopts/include/cxxopts.hpp:526:31: error: conversion to ‘short int’ from ‘int’ may alter its value [-Werror=conversion]
cc1plus: all warnings being treated as errors
src/CMakeFiles/example.dir/build.make:62: recipe for target 'src/CMakeFiles/example.dir/example.cpp.o' failed
make[2]: *** [src/CMakeFiles/example.dir/example.cpp.o] Error 1
CMakeFiles/Makefile2:85: recipe for target 'src/CMakeFiles/example.dir/all' failed
make[1]: *** [src/CMakeFiles/example.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2

and my compiler infomation:

-- cxxopts version 2.2.0
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0

maidamai0 avatar May 25 '19 10:05 maidamai0

That code has a few issues like that. I'll have to go over it and try to write it in a way that doesn't produce those warnings.

jarro2783 avatar May 27 '19 22:05 jarro2783

I think the warnings caused by -Wconversion can be very noisy and produce a lot of problems. In these cases I don't think there is an actual problem. In fact for result * base + digit I can't even see why that happens, because all the values there have the same type.

jarro2783 avatar Jun 15 '19 11:06 jarro2783

I have suppressed these warnings by including the code as follows:

#pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wconversion" #include "cxxopts.hpp" #pragma GCC diagnostic pop

This is of course a workaround and a fix would still be very much appreciated!

klapstoelpiloot avatar Nov 13 '19 07:11 klapstoelpiloot

Why close this? There is still no solution for the issue, only a workaround (those are not the same).

klapstoelpiloot avatar Nov 18 '19 13:11 klapstoelpiloot

Hi, I'm using the latest release (v2.2.1), and I'm also getting a few

In file included from /home/ben/Projects/PSRayTracing/src/main.cpp:4:
/home/ben/Projects/PSRayTracing/src/third_party/cxxopts.hpp: In instantiation of ‘void cxxopts::values::integer_parser(const string&, T&) [with T = unsigned char; std::string = std::__cxx11::basic_string<char>]’:
/home/ben/Projects/PSRayTracing/src/third_party/cxxopts.hpp:630:33:   required from here
/home/ben/Projects/PSRayTracing/src/third_party/cxxopts.hpp:593:33: warning: conversion from ‘int’ to ‘US’ {aka ‘unsigned char’} may change value [-Wconversion]
  593 |         US next = result * base + digit;
      |                   ~~~~~~~~~~~~~~^~~~~~~
/home/ben/Projects/PSRayTracing/src/third_party/cxxopts.hpp: In instantiation of ‘void cxxopts::values::integer_parser(const string&, T&) [with T = signed char; std::string = std::__cxx11::basic_string<char>]’:
/home/ben/Projects/PSRayTracing/src/third_party/cxxopts.hpp:637:33:   required from here
/home/ben/Projects/PSRayTracing/src/third_party/cxxopts.hpp:593:33: warning: conversion from ‘int’ to ‘US’ {aka ‘unsigned char’} may change value [-Wconversion]
/home/ben/Projects/PSRayTracing/src/third_party/cxxopts.hpp: In instantiation of ‘void cxxopts::values::integer_parser(const string&, T&) [with T = short unsigned int; std::string = std::__cxx11::basic_string<char>]’:
/home/ben/Projects/PSRayTracing/src/third_party/cxxopts.hpp:644:33:   required from here
/home/ben/Projects/PSRayTracing/src/third_party/cxxopts.hpp:593:33: warning: conversion from ‘int’ to ‘US’ {aka ‘short unsigned int’} may change value [-Wconversion]
/home/ben/Projects/PSRayTracing/src/third_party/cxxopts.hpp: In instantiation of ‘void cxxopts::values::integer_parser(const string&, T&) [with T = short int; std::string = std::__cxx11::basic_string<char>]’:
/home/ben/Projects/PSRayTracing/src/third_party/cxxopts.hpp:651:33:   required from here
/home/ben/Projects/PSRayTracing/src/third_party/cxxopts.hpp:593:33: warning: conversion from ‘int’ to ‘US’ {aka ‘short unsigned int’} may change value [-Wconversion]
[  4%] Linking CXX executable PSRayTracing

You may be able to remedy this with some static_cast<>() calls.

define-private-public avatar Aug 24 '21 18:08 define-private-public

Changing line 593 to this removed the error:

        US next = static_cast<US>(result * base) + digit;

Does that look correct?

define-private-public avatar Aug 24 '21 18:08 define-private-public

I think this is fixed on master.

jarro2783 avatar Aug 25 '21 10:08 jarro2783

I know you're working on 3.x right now, but I think it might be worth it to sneak the fix in and make a v2.2.2.

define-private-public avatar Aug 25 '21 13:08 define-private-public