在学习如何使用boost精神,凤凰和融合库时,我遇到了这个最小的例子,它不能在msvc(2015,版本14)上编译并且提升1.61.0
#include <boost/spirit/include/karma.hpp>
#include <boost/variant/variant.hpp>
namespace ka = boost::spirit::karma;
struct U /* a kind of union (legacy code)*/
{
bool kind;
double foo; /* if kind = true */
size_t bar; /* if kind = false */
};
typedef boost::variant<double, size_t> UVariant;
namespace boost { namespace spirit { namespace traits {
template<>
struct transform_attribute<U,UVariant,ka::domain>
{
typedef UVariant type;
static type pre(U & u) {
switch (u.kind)
{
case true:
return type(u.foo);
case false:
return type(u.bar);
}
}
};
}}}
typedef std::back_insert_iterator<std::string> iterator;
class grm: public ka::grammar<iterator, U()>
{
public:
grm():grm::base_type(start)
{
start = ka::attr_cast<U,UVariant >(foo | bar);
foo = ka::double_;
bar = ka::uint_;
*/
}
private:
ka::rule<iterator,U()> start;
ka::rule<iterator,double()> foo;
ka::rule<iterator,size_t()> bar;
};
int main(int argc, char * argv[])
{
grm g;
U u;
u.kind = true;
u.foo = 1.0;
std::string generated;
std::back_insert_iterator<std::string> sink(generated);
ka::generate(sink,g,u);
return 0;
}
然后我收到以下错误消息:
错误C2665: '推动::详细::变种:: make_initializer_node ::申请:: initializer_node ::初始化': 5个重载中没有一个可以转换所有参数类型
报告了一个类似的问题here,但我无法理解答案是如何解决问题的,以及这是否真的是同样的问题,因为似乎所有类型都是正确提供的(不需要类型转换)。 / p>
答案 0 :(得分:1)
问题似乎是Spirit没有选择自定义transform_attribute
自定义点。它正在使用默认值,并尝试从boost::variant<double,size_t>
(!!)构建const U
,这显然会失败。
Karma始终在内部使用const值,因此您需要将transform_attribute
的专业化更改为:
namespace boost { namespace spirit { namespace traits {
template<>
struct transform_attribute<const U,UVariant,ka::domain>
^^^^^^^
{
typedef UVariant type;
static type pre(const U & u) {
^^^^^^^
//same as before
}
};
}}}
然后它将由Karma接收并且一切都会起作用。
完整样本(On rextester):
#include <boost/spirit/include/karma.hpp>
#include <boost/variant/variant.hpp>
namespace ka = boost::spirit::karma;
struct U /* a kind of union (legacy code)*/
{
bool kind;
double foo; /* if kind = true */
size_t bar; /* if kind = false */
};
typedef boost::variant<double, size_t> UVariant;
namespace boost { namespace spirit { namespace traits {
template<>
struct transform_attribute<const U,UVariant,ka::domain>
{
typedef UVariant type;
static type pre(const U & u) {
if(u.kind)
{
return type(u.foo);
}
else
{
return type(u.bar);
}
}
};
}}}
typedef std::back_insert_iterator<std::string> iterator;
class grm: public ka::grammar<iterator, U()>
{
public:
grm():grm::base_type(start)
{
start = ka::attr_cast< UVariant >(foo | bar);
foo = ka::double_;
bar = ka::uint_;
}
private:
ka::rule<iterator,U()> start;
ka::rule<iterator,double()> foo;
ka::rule<iterator,size_t()> bar;
};
int main(int argc, char * argv[])
{
grm g;
U u;
u.kind = false;
u.foo = 1.0;
u.bar = 34;
std::string generated;
std::back_insert_iterator<std::string> sink(generated);
ka::generate(sink,g,u);
std::cout << generated << std::endl;
return 0;
}