p-ranav/structopt

GCC with strong compilation flags (Werror) throws errors in include/structopt/is_number.hpp file

AnonymerNiklasistanonym opened this issue · 0 comments

When including structopt in my custom project which adds strong compilation flags for GCC (-pedantic -Wall -Werror -Wextra -Wformat) the following errors appear:

In file included from [...]/structopt/include/structopt/parser.hpp:13,
                 from [...]/structopt/include/structopt/app.hpp:8,
                 from [...]/main.cpp:1:
[...]/structopt/include/structopt/is_number.hpp:9:8: error: type qualifiers ignored on function return type [-Werror=ignored-qualifiers]
    9 | static const bool is_binary_notation(std::string const &input) {
      |        ^~~~~
[...]/structopt/include/structopt/is_number.hpp:14:8: error: type qualifiers ignored on function return type [-Werror=ignored-qualifiers]
   14 | static const bool is_hex_notation(std::string const &input) {
      |        ^~~~~
[...]/structopt/include/structopt/is_number.hpp:19:8: error: type qualifiers ignored on function return type [-Werror=ignored-qualifiers]
   19 | static const bool is_octal_notation(std::string const &input) {
      |        ^~~~~
[...]/structopt/include/structopt/is_number.hpp: In function ‘bool structopt::details::is_valid_number(const string&)’:
[...]/structopt/include/structopt/is_number.hpp:34:12: error: comparison of unsigned expression in ‘>= 0’ is always true [-Werror=type-limits]
   34 |   while (j >= 0 && input[j] == ' ')
      |          ~~^~~~

My main.cpp:

#include <structopt/app.hpp>

int main()
{
  std::cout << "Test" << std::endl;
}

My CMakeLists.txt:

# Set minimum CMake version
cmake_minimum_required(VERSION 3.14)

# Set project name and the programming language
project("test" LANGUAGES CXX)

# Set path to project root
set(VENDOR_DIR "${CMAKE_CURRENT_SOURCE_DIR}/vendor")

option(STRUCTOPT_TESTS "Don't build structopt tests" OFF)
option(STRUCTOPT_SAMPLES "Don't build structopt examples" OFF)
add_subdirectory("${VENDOR_DIR}/structopt" build_extern_structopt)

# Create executable with all provided sources
add_executable(${PROJECT_NAME} main.cpp)

# Link structopt
target_link_libraries(${PROJECT_NAME} PRIVATE structopt::structopt)

# Set library source files compilation flags for different compilers
target_compile_options(${PROJECT_NAME}
                       PRIVATE $<$<CXX_COMPILER_ID:GNU>:
                               -pedantic
                               -Wall
                               -Werror
                               -Wextra
                               -Wformat
                               >
                               $<$<CXX_COMPILER_ID:MSCV>:
                               /W4
                               /Wall
                               /WX
                               >)

# Set C++ version for the local source files
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 20)
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_20)

My build commands build.sh:

#!/usr/bin/env bash

rm -rf build && mkdir -p build && cd build
cmake .. -G "Unix Makefiles"
cmake --build .

The complete log:

-- The CXX compiler identification is GNU 10.1.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: PROJECT_ROOT/build
Scanning dependencies of target test
[ 50%] Building CXX object CMakeFiles/test.dir/main.cpp.o
In file included from PROJECT_ROOT/vendor/structopt/include/structopt/parser.hpp:13,
                 from PROJECT_ROOT/vendor/structopt/include/structopt/app.hpp:8,
                 from PROJECT_ROOT/main.cpp:1:
PROJECT_ROOT/vendor/structopt/include/structopt/is_number.hpp:9:8: error: type qualifiers ignored on function return type [-Werror=ignored-qualifiers]
    9 | static const bool is_binary_notation(std::string const &input) {
      |        ^~~~~
PROJECT_ROOT/vendor/structopt/include/structopt/is_number.hpp:14:8: error: type qualifiers ignored on function return type [-Werror=ignored-qualifiers]
   14 | static const bool is_hex_notation(std::string const &input) {
      |        ^~~~~
PROJECT_ROOT/vendor/structopt/include/structopt/is_number.hpp:19:8: error: type qualifiers ignored on function return type [-Werror=ignored-qualifiers]
   19 | static const bool is_octal_notation(std::string const &input) {
      |        ^~~~~
PROJECT_ROOT/vendor/structopt/include/structopt/is_number.hpp: In function ‘bool structopt::details::is_valid_number(const string&)’:
PROJECT_ROOT/vendor/structopt/include/structopt/is_number.hpp:34:12: error: comparison of unsigned expression in ‘>= 0’ is always true [-Werror=type-limits]
   34 |   while (j >= 0 && input[j] == ' ')
      |          ~~^~~~
cc1plus: all warnings being treated as errors
make[2]: *** [CMakeFiles/test.dir/build.make:82: CMakeFiles/test.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:115: CMakeFiles/test.dir/all] Error 2
make: *** [Makefile:103: all] Error 2

This was tested on a Linux PC (Kernel: 5.8.1-2-MANJARO, Architecture: x86_64 GNU/Linux) with c++ (GCC) 10.1.0 and cmake 3.18.1.

It is just a minor issue I just removed the const qualifiers and the j >= 0 as described in the warnings and then everything worked for me (my main.cpp was at one point more advanced but I trimmed it for this issue - --help and the other features were working fine).