jeudi 5 octobre 2017

How to parse a double from an array of characters using boost::spirit::qi::parse

I can parse numbers from characters stored in std::string, std::vector or std::array. But I am not able to do so when the characters are in a buffer held in std::unique_ptr. I could copy the buffer into a string then to boost spirit but I want to avoid this copy

Here is my attempt:

#include<memory>
#include<array>
#include<iostream>
#include "boost/spirit/include/qi.hpp"


int main()
{
    const int max_size = 40;
    std::array<wchar_t, max_size> numeric1;
    std::wstring src = L"5178120.3663";
    std::wcsncpy(numeric1.data(), src.data(), max_size);

    double num = 0.0;
    boost::spirit::qi::parse(numeric1.begin(), numeric1.end(), num);
    std::cout.precision(15);
    std::cout << num << std::endl; // OK

    std::unique_ptr<wchar_t[]> numeric2(new wchar_t[max_size]);
    std::wcsncpy(numeric2.get(), src.data(), max_size);
    std::wcout << L"ok = " << std::wstring(numeric2.get()) << std::endl; // OK

    boost::spirit::qi::parse(numeric2.data(), max_size, num); // fails to compile 
    std::cout.precision(15);
    std::cout << num << std::endl;

    //  'boost::spirit::qi::parse': no matching overloaded function found

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire