yaml-cpp icon indicating copy to clipboard operation
yaml-cpp copied to clipboard

cannot decode signed char

Open rwols opened this issue 8 years ago • 3 comments

this snippet throws:

    auto node = YAML::Load("-126");
    try {
        const auto y = node.as<signed char>();
        std::cout << "y = " << static_cast<int>(y) << '\n';
    }
    catch (const YAML::TypedBadConversion<signed char>& error) {
        std::cerr << error.what() << '\n';
    }

but -126 fits inside a signed char

rwols avatar Feb 11 '17 20:02 rwols

It should be noted that

std::is_same<char, signed char>::value == false;
std::is_same<char, unsigned char>::value == false;
std::is_same<signed char, unsigned char>::value == false;

there are three types of distinct "char" types. The first one, char, should be seen as a "character", so should be serialized as a "character", whereas signed char and unsigned char should be seen as "numbers" and so should be serialized as "numbers". See: C++14 standard (working draft), section 3.9.1.

rwols avatar Feb 16 '17 20:02 rwols

Somewhat of a "me too", but I've just run into this myself and am somewhat confused. I'm not trying to parse negative numbers, but something like 40 or 29.

Asking for .as<std::uint8_t>() or .as<unsigned char>() gets me the same exception reported by @rwols.

Only .as<unsigned int>() works for some reason.

gavanderhoorn avatar Oct 17 '19 10:10 gavanderhoorn

I'm curious about this one as well.

G-Harmon avatar Sep 03 '21 21:09 G-Harmon