使用 mpl::if_、boost::function 和 typedef 为 void 时出现问题
我是 Boost.MPL 库的新手,并且有一些“初学者问题”
看看这个示例:
template < typename F >
struct A {
typedef boost::function_types::parameter_types<F> P;
typedef typename boost::function_types::result_type<F>::type R;
typedef typename boost::mpl::if_< boost::is_same< R, void >,
boost::function< void ( void ) > ,
boost::function< void ( R ) > >::type TTT;
A() { }
};
int main(int argc, const char *argv[]) {
A<int(int, float)> ok; // working
A<void(int, float)> compile_error; // non-working
return 0;
}
编译时我得到:
xxx.cxx: In instantiation of ‘A<void(int, float)>’:
xxx.cxx:108:25: instantiated from here
xxx.cxx:100:77: error: invalid parameter type
‘boost::mpl::aux::wrapped_type<boost::mpl::aux::type_wrapper<void>
>::type’
xxx.cxx:100:77: error: in declaration ‘A<F>::TTT’
这里有什么问题,我该如何解决它?
据我了解,编译器只应评估 mpl::if_ 的选定部分......
I'm new to the Boost.MPL library, and have some "beginners-problems"
Look at this sample:
template < typename F >
struct A {
typedef boost::function_types::parameter_types<F> P;
typedef typename boost::function_types::result_type<F>::type R;
typedef typename boost::mpl::if_< boost::is_same< R, void >,
boost::function< void ( void ) > ,
boost::function< void ( R ) > >::type TTT;
A() { }
};
int main(int argc, const char *argv[]) {
A<int(int, float)> ok; // working
A<void(int, float)> compile_error; // non-working
return 0;
}
When compiling I get:
xxx.cxx: In instantiation of ‘A<void(int, float)>’:
xxx.cxx:108:25: instantiated from here
xxx.cxx:100:77: error: invalid parameter type
‘boost::mpl::aux::wrapped_type<boost::mpl::aux::type_wrapper<void>
>::type’
xxx.cxx:100:77: error: in declaration ‘A<F>::TTT’
What is the problem here, and how can I solve it?
To my understanding, only the selected part of mpl::if_
should be evaluated by the compiler....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,解释一下这个错误,需要注意的是,在参数列表中使用 typedef 来
void
是一个错误。这两个 GCC 错误报告 (32058 和 9278) 描述问题,指出这是来自的要求标准。所以基本上,根据标准的 §8.3.5/2,这是合法的:
而这是不合法的:
这解释了为什么您首先需要
if_
。现在要解释为什么仍然存在错误,您需要了解 MPL 中的惰性求值仅适用于元函数:只要您不访问元函数内的type
,它就不会被求值。在这里,if_
的参数没有被求值(它们不能,因为它们不是元函数),但这并不意味着它们没有被实例化。为了解决这个问题,您可以将
function
实例嵌入到可以延迟计算的元函数中:这样,错误的
void (typedef_to_void)
永远不会出现。更好的解决方案甚至是针对
void
情况专门化get_function
元函数:不再需要
if_
!First of all, to explain the error, it should be noted that using a typedef to
void
in a parameter list is an error. These two GCC bug reports (32058 and 9278) describe the problem, an point out that this is requirement from the standard.So basically, according to §8.3.5/2 of the standard, this is legal:
while this is not:
This explains why you needed the
if_
in the first place. Now to explain why you still have the error, you need to understand that lazy evaluation in MPL only apply to metafunctions: as long as you don't access thetype
inside a metafunction, it is not evaluated. Here, theif_
's arguments are not evaluated (they could not since they are not metafunctions), but that does not mean that they are not instantiated.To overcome this issue, you could embed the
function
instantiations into metafunctions that could be lazily evaluated:This way, the erroneous
void (typedef_to_void)
never appears.A better solution would even be to specialize the
get_function
metafunction for thevoid
case:No more
if_
needed!