dimanche 1 novembre 2020

Why I can't assign result of std::from_chars to std::tie?

I was thinking, that instead of auto [x,y] I can assign to std::tie made from existing variables but for some reason, it doesn't work. Can anyone tell why? I'm afraid that I don't understand compiler message. It seems to me that it tries to assign integer to the tuple (???)

#include <charconv>
#include <tuple>
int main()
{
    auto s = "123";
    unsigned int val;
    auto [p, err] = std::from_chars(s, s+3, val);       // this compiles
    std::tie(p,err) = std::from_chars(s, s+3, val);     // this doesn't compile
}
> c++ -std=c++17 foo.cpp
foo.cpp: In function ‘int main()’:
foo.cpp:8:50: error: no match for ‘operator=’ (operand types are ‘std::tuple<const char*&, std::errc&>’ and ‘std::__detail::__integer_from_chars_result_type<unsigned int>’ {aka ‘std::from_chars_result’})
     std::tie(p,err) = std::from_chars(s, s+3, val);     // this doesn't compile
                                                  ^
In file included from foo.cpp:2:
/usr/include/c++/8/tuple:1209:7: note: candidate: ‘std::tuple<_T1, _T2>& std::tuple<_T1, _T2>::operator=(const std::tuple<_T1, _T2>&) [with _T1 = const char*&; _T2 = std::errc&]’
       operator=(const tuple& __in)
       ^~~~~~~~
/usr/include/c++/8/tuple:1209:7: note:   no known conversion for argument 1 from ‘std::__detail::__integer_from_chars_result_type<unsigned int>’ {aka ‘std::from_chars_result’} to ‘const std::tuple<const char*&, std::errc&>&’
/usr/include/c++/8/tuple:1216:7: note: candidate: ‘std::tuple<_T1, _T2>& std::tuple<_T1, _T2>::operator=(std::tuple<_T1, _T2>&&) [with _T1 = const char*&; _T2 = std::errc&]’
       operator=(tuple&& __in)

Aucun commentaire:

Enregistrer un commentaire