Implicit conversion does not work for BOOST_STRONG_TYPEDEF and BOOST_SPIRIT_DEBUG_NODE

I defined the rule boost :: spirit :: qi:

boost::spirit::qi::rule<Iterator, Identifier()> id; 

where the identifier is determined by:

 BOOST_STRONG_TYPEDEF(std::string, Identifier) 

but when i use

 BOOST_SPIRIT_DEBUG_NODE(id); 

Unable to compile the following error:

 boost_1_51_0/boost/spirit/home/support/attributes.hpp:1203: error: no match for 'operator<<' in 'out << val' 

and lists the overloaded ostream statements.

Knowing that BOOST_STRONG_TYPEDEF defines a cast operator, should the compiler not implicitly be discarded from the identifier on std :: string when using operator<< ? or is there a restriction that prohibits the compiler from using a type picker when it tries to match another operator (namely operator<< )?

When I define the following statement, it compiles:

 inline std::ostream& operator<<(std::ostream& os, const Identifier& id) { return os << static_cast<std::string const&>(id); } 

I am using gcc4.4.2

+1
source share
1 answer

This has nothing to do with boost, strong_typedef or spirit.

It has a lot to do with type inference for template arguments. In short, when argument types are inferred, implicit conversions never occur [1]

Cf :.

 #include <iostream> #include <string> #include <boost/strong_typedef.hpp> BOOST_STRONG_TYPEDEF(double, X) int main() { std::cout << X(); } 

No problems! Replace double with std::string and it no longer works. What else?

The announcement of the streaming operator is different.

Contrast

 ostream& ostream::operator<<(double); 

To

 template<typename _CharT, typename _Traits, typename _Alloc> inline basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>&, basic_string<_CharT, _Traits, _Alloc> const&) 

The fact that operator overloading is a function template prohibits any implicit conversion.


[1] I suppose that initializer_list may seem a little exception here, with the extension / narrowing, what it can do. Another subject though

+4
source

Source: https://habr.com/ru/post/1203395/


All Articles