- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个(也许)关于复合类型的 boost::lexical_cast 的简单问题(在我的例子中是 std::vector
。
我的第一个模板化字符串化函数版本如下
template <typename T>
std::string stringiy(const T &t)
{
std::ostringstream o;
o<< t;
return o.str();
}
下面是一个工作示例:
vector<int> x(10,-3;
cout << stringify<vector<int> >(x) << endl;
输出 “-3-3-3-3-3-3-3-3”~
但出于性能原因,我想利用 boost::lexical_cast
现在我更改了函数实现:
template <typename T>
std::string stringiy(const T &t)
{
return boost::lexical_cast<string>(t);
}
虽然此方法适用于内置类型,但当它用于 std::vector
如果为 vector 创建一个专门的模板,问题仍然存在(它不编译)
template <typename T>
std::string stringiy(const std::vector<T> &t)
{
vector<string> strret = num2str(t);
string r;
for ( vector<string>::iterator iter = strret.begin(); iter!=strret.end(); ++iter )
r.append(*iter);
return r;
}
有什么建议吗?
g++-4.5, ubuntu 11.10 amd64
In file included from Util.h:41:0,
from testLexicalCast.cpp:49:
/usr/include/boost/lexical_cast.hpp: In member function ‘bool boost::detail::lexical_stream<Target, Source, Traits>::operator<<(const Source&) [with Target = std::basic_string<char>, Source = std::vector<double>, Traits = std::char_traits<char>]’:
/usr/include/boost/lexical_cast.hpp:1151:13: instantiated from ‘Target boost::detail::lexical_cast(typename boost::call_traits<Source>::param_type, CharT*, size_t) [with Target = std::basic_string<char>, Source = std::vector<double>, bool Unlimited = true, CharT = char, typename boost::call_traits<Source>::param_type = const std::vector<double>&, size_t = long unsigned int]’
/usr/include/boost/lexical_cast.hpp:1174:77: instantiated from ‘Target boost::lexical_cast(const Source&) [with Target = std::basic_string<char>, Source = std::vector<double>]’
Util.h:211:43: instantiated from ‘std::string util::stringify(const T&) [with T = std::vector<double>, std::string = std::basic_string<char>]’
testLexicalCast.cpp:72:53: instantiated from here
/usr/include/boost/lexical_cast.hpp:595:48: error: no match for ‘operator<<’ in ‘((boost::detail::lexical_stream<std::basic_string<char>, std::vector<double>, std::char_traits<char> >*)this)->boost::detail::lexical_stream<std::basic_string<char>, std::vector<double>, std::char_traits<char> >::stream << input’
/usr/include/c++/4.5/ostream:108:7: note: candidates are: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:117:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>, std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
/usr/include/c++/4.5/ostream:127:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:165:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:169:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:173:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/bits/ostream.tcc:91:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.5/ostream:180:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/bits/ostream.tcc:105:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.5/ostream:191:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:200:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:204:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:209:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:213:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:221:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/ostream:225:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.5/bits/ostream.tcc:119:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
make: *** [testLexicalCast.o] Error 1
最佳答案
您可能已经声明了您的 operator<<
对于全局命名空间中的 vector ,以及 boost::lexical_cast
只会查看 boost::
命名空间和 std::
命名空间,其中 ostream
被定义为。尝试将您的重载添加到 std::
通过包装你的命名空间 operator<<
namespace std { ... }
中的声明和定义.
关于c++ - boost::lexical_cast 和非内置类型的字符串化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8490814/
我看到了其他 boost::lexical_cast 问题的一些答案,断言以下是可能的: bool b = boost::lexical_cast("true"); 这不适用于 g++ 4.4.3 b
我已经看到其他 boost::lexical_cast 问题的一些答案,这些问题断言以下是可能的: bool b = boost::lexical_cast("true"); 这不适用于 g++ 4.
boost::lexical_cast 在将字符串转换为 int8_t 时抛出异常,但 int32_t - 正常。 int8_t 有什么问题? #include #include #include
我正在尝试扩展 lexical_cast 以处理 string->cv::Point 转换,代码如下: #include #include #include #include #include
我在这个话题中看到C++ convert hex string to signed integer boost::lexical_cast 可以将字符串中的十六进制转换为另一种类型(int、long.
我想扩展 lexical_cast vector 的方法类型,但它不起作用。我尝试了以下代码: #include namespace boost { template <> inli
我是 boost::lexical_cast 的新手,对其内部结构了解甚少。我正在尝试进行以下转换: string someString = boost::lexical_cast(sourceStr
我正在尝试构建一个将程序设置存储为 std::map 的类。由于所有程序设置都存储为字符串,因此我想要一个可以返回转换为相关类型的程序设置的访问器方法。我刚接触 C++ 模板,这是我的第一次尝试: c
我在两个不同的设备上有相同版本的 boost ,但行为不同 lexical_cast("-1") 文档指出它应该给我 INT_MAX(2 的补码翻转)但是在一台机器上我得到一个异常抛出而在另一台机器上
我有一个(也许)关于复合类型的 boost::lexical_cast 的简单问题(在我的例子中是 std::vector。 我的第一个模板化字符串化函数版本如下 template std::str
我遇到了转换问题,希望得到您的帮助。我正在使用 gcc4 编译器,但我对 gcc4 的使用非常有限。 我想将 std::string 转换为 double。 std::string aQuantity
我有一种情况,我正在获取命令行参数并使用 boost::lexical_cast(my_param) .我希望 my_param 的负值会导致 lexical_cast 抛出,但它会愉快地转换它们,-
我正在使用 boost::lexical_cast(double)用于将 double 转换为字符串,生成 JSON 序列化字节流,即(在远程端)由 .NET 解析。 我能够强制 .NET 使用 In
我实际上没能在 boost 文档中找到这个问题的答案。我对在多线程环境中使用 atof 有点偏执,所以一个建议是用 lexical_cast 替换调用。 lexical_cast 是线程安全的吗? 最
我正在为 C++ 使用 boost 库,函数 lexical_cast 的行为非常奇怪。如果我执行 lexical_cast("0.07513994") 它工作正常,但如果我使用我需要转换的变量,它会
编译以下内容: // file main.cpp #include #include int main() { boost::lexical_cast( 656.16 ); ret
以 bool 变量作为输入的 boost::lexical_cast 的输出预计为 0 或 1 值(value)。但是我得到了不同的值。 这不是正常发生的事情。让我们看一下我编写的示例代码: #inc
我查看了 lexical_cast.hpp 的困惑情况,但它仍然让我无法理解。 lexical_cast 的“基本定义”采用模板源和目标,如何能够接受诸如 lexical_cast("7") 之类的语
我正在参加一项挑战,为了切入主题,在我的程序中的一个地方,我需要将字符串转换为整数。我试过 boost::lexical_cast 但不幸的是它太慢了www。我想是因为它执行的所有检查。我需要的是无需
忽略 boost::lexical_cast 的异常是否安全?将 int 转换为 std::string 时? 最佳答案 将 int 转换为 std::string 时词法转换引发的异常与转换无关,但
我是一名优秀的程序员,十分优秀!