Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 977
stilz Avatar asked Feb 22 '23 21:02

stilz


2 Answers

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

like image 140
neodelphi Avatar answered Mar 06 '23 23:03

neodelphi


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

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

Demo : http://ideone.com/ynf15

like image 43
Nawaz Avatar answered Mar 06 '23 22:03

Nawaz