开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜