C++ 变量不能同时处理字符串和wstring

C++ 变量不能同时处理字符串和wstring,c++,wstring,boost-variant,C++,Wstring,Boost Variant,在尝试在库中添加对UTF-8语言环境的支持时,我添加了 将类型std::wstring添加到保存值的boost::variant 在这一点上,我开始在boost::variant: Blockquote/opt/TWWfsw/libboost147/include/boost/variant/detail/variant_io.hpp:在成员函数“void boost::detail::variant::printer::operator()(const T&)const[with T=std:

在尝试在库中添加对UTF-8语言环境的支持时,我添加了 将类型
std::wstring
添加到保存值的
boost::variant

在这一点上,我开始在
boost::variant
:

Blockquote/opt/TWWfsw/libboost147/include/boost/variant/detail/variant_io.hpp:在成员函数“void boost::detail::variant::printer::operator()(const T&)const[with T=std::basic_string,std::allocator>,OStream=std::basic_OStream>”:
/opt/TWWfsw/libboost147/include/boost/variant/variant.hpp:858:从“typename Visitor::result_type boost::detail::variant::invoke_Visitor::internal_visit(T&,int)[with T=const std::basic_string,std::allocator>,Visitor=boost::detail::variant::printer>]

Cursor.H:84:从此处实例化
/opt/TWWfsw/libboost147/include/boost/variant/detail/variant_io.hpp:64:错误:与“操作员”不匹配此)->boost::detail::variant::printer>>::out您的问题不是
boost::variant
无法同时处理
字符串和
字符串。问题是,
wcout
无法处理
std::string
。有关这方面的更多信息,请参见为什么wcout您的问题不是
boost::variant
无法同时处理
string
wstring
。问题是,
wcout
无法处理
std::string
。有关这方面的更多信息,请参见为什么wcout基于@John Bandela的答案:他实际上是reight。我会再解释一点罪犯的情况


注意,
std::cout基于@johnbandela的答案:他实际上是reight。我会再解释一点罪犯的情况


请注意,
std::cout,因此即使变量不包含std::string值,也不能在“wcout”中使用它,而不是使用“wcout”问题是,如果我修改程序使用std::cout并输出int值变量,同样的错误也会发生。John-你的答案是正确的-但这不是这个问题的内容。问题在于boost::variant的实现,它似乎无法处理“运算符”。因此,即使variant不包含std::string值,也不能在“wcout”中使用它。不是使用“wcout”是问题所在,如果我修改程序以使用std::cout并输出int值的variant,同样的错误也会发生。约翰-你的答案是正确的-但这不是这个问题的内容。问题在于boost::variant的实现,它似乎无法处理“操作员自从我提出这个问题以来,我对编码有了更多的了解,现在我是UTF-8的狂热爱好者。如果您感兴趣,请阅读我提出的这个问题,我学到了更多关于编码的知识,现在我是UTF-8 Everywhere的狂热者。如果你感兴趣,请阅读
#include <string>
#include <iostream>
#include <boost/variant.hpp>
#define NO_STRING 1

int main (int argc, char** argv)
{
#if defined(NO_STRING)
  boost::variant<int, std::wstring> v;
#else
  boost::variant<int, std::wstring, std::string> v;
#endif
  v = 3;
  std::wcout << v << std::endl;
  std::wstring T(L"wide char literal");
  v = T;
  std::wcout << v << std::endl;
  return 0;
}
void variant::visit(T operation){
  if(contained is T1) operation(static_cast<T1>(contained));
  else if(contained is T2) operation(static_cast<T2>(contained));
  ...
}
#include <string>
#include <iostream>
#include <boost/variant.hpp>

std::wstring stringToWString(const std::string& v){
    // Only works for single-byte strings. Do it better!
    std::wstring result(v.begin(), v.end());
    return result;
}

struct visitor: boost::static_visitor<void>{
    template<typename T>
    void operator()(const T& v) const { std::wcout << v; }
    void operator()(const std::string& v) const { std::wcout << stringToWString(v); }
};

int main (int argc, char** argv)
{
  boost::variant<int, std::wstring, std::string> v;
  v = 3;
  boost::apply_visitor(visitor(), v);
  std::wcout << std::endl;
  std::wstring T(L"wide char literal");
  v = T;
  boost::apply_visitor(visitor(), v);
  std::wcout << std::endl;
  v = std::string("Narrow string");
  boost::apply_visitor(visitor(), v);
  std::wcout << std::endl;
  return 0;
}