我有一个非常简单的解析器规则(对于 AXE),如下所示:
auto space = axe::r_lit(' ');
auto spaces = space & space & space;
最后一行在 VC2010 中按预期编译和工作,但在 gcc 4.6 中给出了一个奇怪的错误:
parsers.cpp:68:34: error: conversion from
'axe::r_and_t<
axe::r_and_t<axe::r_char_t<char>&, axe::r_char_t<char>&>,
axe::r_char_t<char>&
>' to non-scalar type
'axe::r_and_t<
axe::r_and_t<axe::r_char_t<char>&, axe::r_char_t<char>&>&,
axe::r_char_t<char>&
>' requested
我想知道,这是否是 gcc 中的一个(已知)错误,以及是否有可能通过 auto
声明获得转换错误。 auto
的推导类型不应该始终与初始值设定项的类型完全相同吗?
斧头 overloads operator&像这样:
template<class R1, class R2>
r_and_t<
typename std::enable_if<
is_rule<typename std::remove_reference<R1>::type>::value, R1>::type,
typename std::enable_if<
is_rule<typename std::remove_reference<R2>::type>::value, R2>::type
>
operator& (R1&& r1, R2&& r2)
{
return r_and_t<R1, R2>(std::forward<R1>(r1), std::forward<R2>(r2));
}
我无法将问题简化为一个简短的测试用例,不幸的是,每次我尝试使用简单的示例时,它都能编译。
最佳答案
auto
并不总是完全是初始值设定项的类型,因为 auto
正在删除引用,使其在您期望 的地方键入
。如果您需要引用 - 拼写 T
>T&auto&
。
关于c++ - 自动类型推导可能会导致转换错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6399363/