Can't stream std::endl with overloaded operator<<() for std::variant

前端 未结 3 509
不思量自难忘°
不思量自难忘° 2020-12-14 07:47

This answer describes how to stream a standalone std::variant. However, it doesn\'t seem to work when std::variant is stored in a std::unorde

3条回答
  •  佛祖请我去吃肉
    2020-12-14 08:45

    The problem is the std::endl but I am puzzled why your overload is a better match than the one from std::basic_ostream::operator<<, see godbolt live example:

    :29:12: note: in instantiation of template class 'std::variant<>' requested here
            << std::endl;
               ^
    

    and removing the std::endl indeed fixes the problem, see it live on Wandbox.

    As alfC points out altering your operator to disallow an empty variant does indeed fix the issue, see it live:

    template
    std::ostream& operator<<(std::ostream& os, const std::variant& v)
    

提交回复
热议问题