Skip to content

misleading fix-it with bugprone-unintended-char-ostream-output in templated code #135558

@firewave

Description

@firewave

The fix-it is problematic if it is a templated type and should probably be omitted in that case:

#include <cstdint>
#include <sstream>

template<typename T>
void func(const T& data)
{
    std::ostringstream ostr;
    ostr << data;
}

void f()
{
    func((char)0);
    func((int8_t)0);
    func("");
}
<source>:8:10: warning: 'signed char' passed to 'operator<<' outputs as character instead of integer. cast to 'unsigned int' to print numeric value or cast to 'char' to print as character [bugprone-unintended-char-ostream-output]
    8 |     ostr << data;
      |          ^  ~~~~
      |             static_cast<int>(data)

https://godbolt.org/z/93axK1E4M

Originally posted by @firewave in #127720 (comment)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions