Skip to content

Compilation errors: narrowing conversion in binhack.hpp #6

@hackerb9

Description

@hackerb9

When cd'ing to src and running make on a system running Debian 10 (stable), the compilation dies with error messages that look like this:

make[2]: Entering directory '/tmp/mksdiso/src/binhack'
g++ -O3 -fPIC -c src/binhack.cpp -o obj/binhack.o
In file included from src/binhack.cpp:1:
src/binhack.hpp:772:1: error: narrowing conversion of ‘160’ from ‘int’ to ‘char’ inside { } [-Wnarrowing]
 };
 ^
src/binhack.hpp:772:1: error: narrowing conversion of ‘160’ from ‘int’ to ‘char’ inside { } [-Wnarrowing]
src/binhack.hpp:772:1: error: narrowing conversion of ‘160’ from ‘int’ to ‘char’ inside { } [-Wnarrowing]
src/binhack.hpp:772:1: error: narrowing conversion of ‘160’ from ‘int’ to ‘char’ inside { } [-Wnarrowing]

The problem appears to be that the literal constants (0xA0) are not specified to be of type char, and the C++ 11 standard states that such code is "not well formed".

Here's what the manpage for gcc has to say.

-Wno-narrowing (C++ and Objective-C++ only)
   
    For C++11 and later standards, narrowing conversions are diagnosed by
    default, as required by the standard. A narrowing conversion from a
    constant produces an error, and a narrowing conversion from a non-constant
    produces a warning, but -Wno-narrowing suppresses the diagnostic. Note that
    this does not affect the meaning of well-formed code; narrowing conversions
    are still considered ill-formed in SFINAE contexts.

    With -Wnarrowing in C++98, warn when a narrowing conversion prohibited by
    C++11 occurs within ‘{ }’, e.g.

    int i = { 2.2 }; // error: narrowing from double to int

    This flag is included in -Wall and -Wc++11-compat.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions