【发布时间】:2013-04-27 06:55:31
【问题描述】:
在尝试在库中添加对 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: 错误: 'operator >)this)->boost::detail::variant::printer > >::out_ /opt/TWWfsw/gcc44/include/c++/ostream:108:注意:候选人是:std::basic_ostream<_chart _traits>& std::basic_ostream<_chart _traits>::operator& ()(std::basic_ostream<_chart _traits>&)) [with _CharT = char, _Traits = std::char_traits]
等
此示例使用带有 g++ 4.4 的 boost-1.47。
#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;
}
这个程序会输出:
3
宽字符字面量
但是如果#define被删除,并且string和wstring都在变量模板参数中,同样的错误结果。
我的问题是,我可以创建一些可以满足这个缺失定义的东西吗,比如模板特化?
也许定义一个将宽字符串转换为窄字符串的变体访问者? (不是一般的解决方案,但在我的情况下可以缩小范围)
问题来自在定义两个字符串时将<< 与变体一起使用。即使我只通过变体输出int,它也不会编译。
【问题讨论】:
标签: c++ wstring boost-variant