【发布时间】:2013-01-31 17:20:26
【问题描述】:
我已经定义了一个 boost::spirit::qi 规则:
boost::spirit::qi::rule<Iterator, Identifier()> id;
其中标识符定义为:
BOOST_STRONG_TYPEDEF(std::string, Identifier)
但是当我使用时
BOOST_SPIRIT_DEBUG_NODE(id);
编译失败,出现如下错误:
boost_1_51_0/boost/spirit/home/support/attributes.hpp:1203: error: no match for 'operator<<' in 'out << val'
它列出了ostream的重载运算符。
知道 BOOST_STRONG_TYPEDEF 为原始类型定义了一个强制转换运算符,不应该
使用operator<< 时,编译器从标识符隐式转换为std::string?或者是否存在阻止编译器在尝试匹配另一个运算符(即 operator<< )时应用类型的强制转换运算符的限制?
当我定义以下运算符时,它会编译:
inline std::ostream& operator<<(std::ostream& os, const Identifier& id)
{
return os << static_cast<std::string const&>(id);
}
我正在使用 gcc4.4.2
【问题讨论】:
-
当然是
static_cast<std::string const&>。否则会发生无意义的复制
标签: c++ boost operator-overloading boost-spirit