在尝试在库中添加对 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 >]'
< SNIP SNIP >
Cursor.H:84: 从这里实例化 /opt/TWWfsw/libboost147/include/boost/variant/detail/variant_io.hpp:64: 错误: 'operator<<' in '((const boost::detail ::变体::打印机 >>)this)->boost::detail::variant::printer > >::out_ <<operand'
/opt/TWWfsw/gcc44/include/c++/ostream:108: 注意:候选者是:std::basic_ostream<_CharT , _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& ( )(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,它也不会编译。