If we ignore the attribute, this test::char_
will do the work.
namespace test {
struct any_char: x3::char_parser<any_char> {
static bool const has_attribute = false;
template <typename Char, typename Context>
bool test(Char ch_, Context const&) const {
return true;
}
};
auto const char_ = any_char{};
}
But what if we want to get the attribute from test::char_
? I don't know how to set attribute_type correctly because this attribute_type should be
typename std::iterator_traits<Iterator>::value_type
This is not a problem in spirit qi because qi parser has a template attribute struct where we can get the char type from Iterator.
Edit: Thank sehe for answering this. Automatic attribute propagation works for test::char_
, but it still cannot propagate correct attribute for compound attribute say +test::char_
.
Edit2: I would like to use this test::char_ to replace various char_encoding::char_ in x3. For example I have a simple parser function foo. The input string could be ascii encoded string or wide char encoded string. Therefore I can use this test::char_ instead of x3::char_ and x3::standard_wide::char_ with a #IFDEF everywhere.
#ifdef NARROW_CHAR
using _char = char;
#define _STR(str) str
#else
using _char = wchar_t;
#define _STR(str) L ## str
#endif
bool foo(std::basic_string<_char> const& input, std::basic_string<_char>& attr) {
return x3::parse(
input.begin(),
input.end(),
+(test::char_ - _STR('X')),
attr);
}
Edit3: Setting attribute_type to void or x3::unused_type will make the test::char_ propagate correct attribute.
namespace test {
struct any_char: x3::char_parser<any_char> {
using attribute_type = void; // or x3::unused_type
static bool const has_attribute = true;
template <typename Char, typename Context>
bool test(Char ch_, Context const&) const {
return true;
}
};
auto const char_ = any_char{};
}
Edit4: Probably http://coliru.stacked-crooked.com/a/0a487591fbaedef4 from sehe is a better idea compared to this test::char_. Thanks.