Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

X3: Vector of variant breaks string attributes if variant also contains character type #776

Open
sehe opened this issue Oct 13, 2023 · 0 comments

Comments

@sehe
Copy link

sehe commented Oct 13, 2023

Observed from here, reproducer:

Live On Coliru:

#include <boost/spirit/home/x3.hpp>
#include <iomanip>
#include <iostream>

namespace x3 = boost::spirit::x3;
constexpr static inline std::string_view sample = R"("asdf" "xxx" "yyy")";

template <typename CharType> size_t test() {
    static auto const qstr                      //
        = x3::rule<class _, std::string>{"str"} //
        = x3::lexeme['"' >> *~x3::char_('"') >> '"'];

    using Var = boost::variant<std::string, CharType>;

    std::vector<Var> data;
    phrase_parse(sample.begin(), sample.end(), *qstr, x3::space, data);
    return data.size();
}

#define TEST(CharType) \
    std::cout << std::setw(13) << #CharType << " -> " << test<CharType>() << std::endl;

int main() {
    TEST(uint8_t);
    TEST(int8_t);
    TEST(signed char);
    TEST(unsigned char);

    TEST(char);
}

Prints

      uint8_t -> 3
       int8_t -> 3
  signed char -> 3
unsigned char -> 3
         char -> 10

As we can see, having char inside the variant breaks string parsing, instead treating the vector as the "string container", filling it with the individual characters.

This breaking change happened in c60d93f which was "bug fix for x3 where container attribute is substitute for the container value type" (indeed the output was consistent with boost 1.58.0)

I think the exception introduced there should not apply here as the rule declares an attribute of std::string explicitly, and as such should not expect vector<variant<..., char>> to be a match.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant