结合 boost::lexical_cast 和 std::transform
我想写这样的东西,它无法编译:
std::vector<A> as;
std::vector<B> bs( as.size() );
std::transform( as.beginn(), as.end(), bs.begin(), boost::lexical_cast<B> );
但这不起作用,所以我创建了一个函子来为我做这件事:
template<typename Dest>
struct lexical_transform
{
template<typename Src>
Dest operator()( const Src& src ) const
{
return boost::lexical_cast<Dest>( src );
}
};
有没有更简单的方法来做到这一点?
I would like to write something like this, which cannot be compiled:
std::vector<A> as;
std::vector<B> bs( as.size() );
std::transform( as.beginn(), as.end(), bs.begin(), boost::lexical_cast<B> );
But this is not working, so I created a functor which is doing this for me:
template<typename Dest>
struct lexical_transform
{
template<typename Src>
Dest operator()( const Src& src ) const
{
return boost::lexical_cast<Dest>( src );
}
};
Is there an easier way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
lexical_cast
有两个模板参数:目标类型和源类型。在正常使用情况下,秒是从调用中推导出来的。但是,这里您想要获取函数的地址,并且需要指定所有模板参数:
lexical_cast
has two template arguments: target type and source type. Under normal usage, the second is deduced from the call.However, here you want to take the address of the function, and you need to specify all the template arguments:
如果您经常做这种事情,您可能需要考虑 Boost.Convert 库(尚未被 Boost 接受)。请参阅手册中的示例:
If you do this kind of thing a lot you might want to consider the Boost.Convert library (not an accepted part of Boost yet). See this example from the manual: