我有一个模板函数,它将字符串转换为数字,如下所示:
template <typename RetType,
typename Parser =
typename boost::spirit::traits::create_parser<RetType>::type>
inline std::enable_if_t<std::is_arithmetic<RetType>::value, RetType>
cast(const std::string &input)
{
RetType result;
if(input.empty())
{
// handle this
}
auto itBeg = input.cbegin();
auto itEnd = input.cend();
if(!bsq::parse(itBeg, itEnd, Parser(), result) || itBeg != itEnd)
{
// handle that
}
return result;
}
现在我想创建一个类似于上面的函数,它将解析表示某个基数中的数字的字符串
template <typename RetType, unsigned Radix,
typename Parser =
typename boost::spirit::traits::create_parser<RetType>::type>
inline std::enable_if_t<std::is_arithmetic<RetType>::value, RetType>
cast(const std::string &input)
{
RetType result;
if(input.empty())
{
// handle this
}
auto itBeg = input.cbegin();
auto itEnd = input.cend();
if(!bsq::parse(itBeg, itEnd, Parser<RetType, Radix, 1 - 1>() /*something like this*/, result) || itBeg != itEnd)
{
// handle that
}
return result;
}
当然,Parser是无效的,但是用radix定义“自动”算术解析器的正确方法是什么?
答案 0 :(得分:5)
我直接使用qi::int_parser<>
:
<强> Live On Coliru 强>
#include <boost/spirit/include/qi.hpp>
#include <type_traits>
template <typename RetType, unsigned Radix = 10, typename Parser = typename boost::spirit::qi::int_parser<RetType, Radix> >
inline typename std::enable_if<std::is_arithmetic<RetType>::value, RetType>::type
cast(const std::string &input)
{
RetType result;
if(input.empty())
{
// handle this
}
auto itBeg = input.cbegin();
auto itEnd = input.cend();
if(!boost::spirit::qi::parse(itBeg, itEnd, Parser(), result) || itBeg != itEnd)
{
// handle that
throw "oops";
}
return result;
}
int main() {
std::cout << cast<int> ("10") << "\n";
std::cout << cast<int, 2>("10") << "\n";
std::cout << cast<int, 8>("10") << "\n";
std::cout << cast<int, 16>("10") << "\n";
std::cout << cast<int, 16>("ee") << "\n";
}
打印
10
2
8
16
238
提示:要非常准确,您可能需要检测签名/无符号类型并相应地使用
uint_parser<>