开发者

Does boost::variant work with std::string?

开发者 https://www.devze.com 2023-04-07 10:33 出处:网络
I\'ve written a simple program in C++ with use of boost::variant. Program\'s code is presented below.

I've written a simple program in C++ with use of boost::variant. Program's code is presented below.

    #include <string>
    #include <iostream>
    #include <boost/variant.hpp>

    int main (int argc, char** argv)
    {
        boost::variant<int, std::wstring> v;
        v = 3;
        std::cout << v << std::endl;
        return 0;
    }

But when I try to compile this with command

g++ main.cpp -o main -lboost_system

i get

/usr/include/boost/variant/detail/variant_io.hpp:64: error: no match for ‘operator<<’ in ‘((const boost::detail::variant::printer<std::basic_ostream<char, std::char_traits<char> > >*)this)->boost::detail::variant::printer<std::basic_ostream<char, std::char_traits<char> > >::out_ << operand’

followed by a bunch of开发者_JAVA百科 candidate functions.

What I'm missing? The funny thing is When I use std::string instead of std::wstring everything works great.

Thanks in advance.


The problem is that wstring cannot be << in cout. Try using wcout instead. This is not a problem with the variant.


Use wcout, not cout. Because you're using wstring, not string.

std::wcout <<  v << std::endl;
   //^^^^ note

Demo : http://ideone.com/ynf15

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号