C++:为命名空间内的类型定义的无符号整数向量提供流插入运算符
首先:我不是 C++ 专家,但我正在为低级 C 库编写 C++ 绑定,并且需要一些帮助才能采取正确的方法。
以下是将我面临的问题抽象为一个简单的示例(请参阅评论):
#include <iostream>
#include <vector>
#include <iterator>
#include <map>
// We have a namespace N.
namespace N {
// And a nested namespace NN.
namespace NN {
// A scoped enum E.
enum class E {
FIRST = 1,
SECOND,
};
// And a typedef for a vector holding E values.
using EV = ::std::vector<E>;
// We want to make E printable so let's provide a mapping of values
// to human-readable names ...
const ::std::map<E, ::std::string> E_names = {
{ E::FIRST, "FIRST" },
{ E::SECOND, "SECOND" }
};
// ... and stream insertion operator for the enum ...
::std::ostream& operator<<(::std::ostream& out, E e)
{
out << E_names.at(e);
return out;
}
// ... and for the vector.
::std::ostream& operator<<(::std::ostream& out, const EV& ev)
{
out << "(";
::std::copy(ev.begin(), ::std::prev(ev.end()),
::std::ostream_iterator<E>(out, ", "));
out << ev.back();
out << ")";
return out;
}
} /* namespace NN */
// Now back in namespace N, let's define the following class:
class A
{
public:
explicit A(void) {}
void m(const NN::EV& ev) {}
NN::EV get_ev(void) const { return NN::EV({ NN::E::FIRST, NN::E::SECOND}); }
};
// And its own stream insertion operator:
::std::ostream& operator<<(::std::ostream& out, const A& a)
{
out << "A(" << a.get_ev() << ")";
return out;
}
} /* namespace N */
// And do the following in main():
int main(int argc, char **argv)
{
::std::cout << N::NN::E::FIRST << ::std::endl;
::std::cout << N::NN::EV({ N::NN::E::FIRST, N::NN::E::SECOND }) << ::std::endl;
::std::cout << N::A().get_ev() << ::std::endl;
::std::cout << N::A() << ::std::endl;
return 0;
}
上面的工作正常。现在让我们按如下方式扩展示例:
#include <iostream>
#include <vector>
#include <iterator>
#include <map>
namespace N {
namespace NN {
enum class E {
FIRST = 1,
SECOND,
};
using EV = ::std::vector<E>;
// Let's add another typedef for a vector holding unsigned integers.
using UV = ::std::vector<unsigned int>;
const ::std::map<E, ::std::string> E_names = {
{ E::FIRST, "FIRST" },
{ E::SECOND, "SECOND" }
};
::std::ostream& operator<<(::std::ostream& out, E e)
{
out << E_names.at(e);
return out;
}
::std::ostream& operator<<(::std::ostream& out, const EV& ev)
{
out << "(";
::std::copy(ev.begin(), ::std::prev(ev.end()),
::std::ostream_iterator<E>(out, ", "));
out << ev.back();
out << ")";
return out;
}
// And define the following stream insertion operator.
::std::ostream& operator<<(::std::ostream& out, const UV& uv)
{
out << "(";
::std::copy(uv.begin(), ::std::prev(uv.end()),
::std::ostream_iterator<unsigned int>(out, ", "));
out << uv.back();
out << ")";
return out;
}
} /* namespace NN */
class A
{
public:
explicit A(void) {}
void m(const NN::EV& ev) {}
NN::EV get_ev(void) const { return NN::EV({ NN::E::FIRST, NN::E::SECOND}); }
NN::UV get_uv(void) const { return NN::UV({ 10, 20 }); }
};
::std::ostream& operator<<(::std::ostream& out, const A& a)
{
out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
return out;
}
} /* namespace N */
int main(int argc, char **argv)
{
::std::cout << N::NN::E::FIRST << ::std::endl;
::std::cout << N::NN::EV({ N::NN::E::FIRST, N::NN::E::SECOND }) << ::std::endl;
::std::cout << N::A().get_ev() << ::std::endl;
::std::cout << N::A().get_uv() << ::std::endl;
::std::cout << N::A() << ::std::endl;
return 0;
}
现在以下 splat 是由 gcc 生成的:
ostest.cpp: In function ‘std::ostream& N::operator<<(std::ostream&, const N::A&)’:
ostest.cpp:68:36: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’})
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^~ ~~~~~~~~~~
| | |
| | N::NN::UV {aka std::vector<unsigned int>}
| std::basic_ostream<char>
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:108:7: note: candidate: ‘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>]’
108 | operator<<(__ostream_type& (*__pf)(__ostream_type&))
| ^~~~~~~~
/usr/include/c++/10/ostream:108:36: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&)’ {aka ‘std::basic_ostream<char>& (*)(std::basic_ostream<char>&)’}
108 | operator<<(__ostream_type& (*__pf)(__ostream_type&))
| ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/10/ostream:117:7: note: candidate: ‘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>]’
117 | operator<<(__ios_type& (*__pf)(__ios_type&))
| ^~~~~~~~
/usr/include/c++/10/ostream:117:32: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&)’ {aka ‘std::basic_ios<char>& (*)(std::basic_ios<char>&)’}
117 | operator<<(__ios_type& (*__pf)(__ios_type&))
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/10/ostream:127:7: note: candidate: ‘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>]’
127 | operator<<(ios_base& (*__pf) (ios_base&))
| ^~~~~~~~
/usr/include/c++/10/ostream:127:30: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘std::ios_base& (*)(std::ios_base&)’
127 | operator<<(ios_base& (*__pf) (ios_base&))
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/10/ostream:166:7: note: candidate: ‘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>]’
166 | operator<<(long __n)
| ^~~~~~~~
/usr/include/c++/10/ostream:166:23: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘long int’
166 | operator<<(long __n)
| ~~~~~^~~
/usr/include/c++/10/ostream:170:7: note: candidate: ‘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>]’
170 | operator<<(unsigned long __n)
| ^~~~~~~~
/usr/include/c++/10/ostream:170:32: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘long unsigned int’
170 | operator<<(unsigned long __n)
| ~~~~~~~~~~~~~~^~~
/usr/include/c++/10/ostream:174:7: note: candidate: ‘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>]’
174 | operator<<(bool __n)
| ^~~~~~~~
/usr/include/c++/10/ostream:174:23: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘bool’
174 | operator<<(bool __n)
| ~~~~~^~~
In file included from /usr/include/c++/10/ostream:784,
from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/bits/ostream.tcc:91:5: note: candidate: ‘std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]’
91 | basic_ostream<_CharT, _Traits>::
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/10/bits/ostream.tcc:92:22: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘short int’
92 | operator<<(short __n)
| ~~~~~~^~~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:181:7: note: candidate: ‘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>]’
181 | operator<<(unsigned short __n)
| ^~~~~~~~
/usr/include/c++/10/ostream:181:33: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘short unsigned int’
181 | operator<<(unsigned short __n)
| ~~~~~~~~~~~~~~~^~~
In file included from /usr/include/c++/10/ostream:784,
from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/bits/ostream.tcc:105:5: note: candidate: ‘std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]’
105 | basic_ostream<_CharT, _Traits>::
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/10/bits/ostream.tcc:106:20: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘int’
106 | operator<<(int __n)
| ~~~~^~~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:192:7: note: candidate: ‘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>]’
192 | operator<<(unsigned int __n)
| ^~~~~~~~
/usr/include/c++/10/ostream:192:31: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘unsigned int’
192 | operator<<(unsigned int __n)
| ~~~~~~~~~~~~~^~~
/usr/include/c++/10/ostream:201:7: note: candidate: ‘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>]’
201 | operator<<(long long __n)
| ^~~~~~~~
/usr/include/c++/10/ostream:201:28: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘long long int’
201 | operator<<(long long __n)
| ~~~~~~~~~~^~~
/usr/include/c++/10/ostream:205:7: note: candidate: ‘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>]’
205 | operator<<(unsigned long long __n)
| ^~~~~~~~
/usr/include/c++/10/ostream:205:37: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘long long unsigned int’
205 | operator<<(unsigned long long __n)
| ~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/10/ostream:220:7: note: candidate: ‘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>]’
220 | operator<<(double __f)
| ^~~~~~~~
/usr/include/c++/10/ostream:220:25: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘double’
220 | operator<<(double __f)
| ~~~~~~~^~~
/usr/include/c++/10/ostream:224:7: note: candidate: ‘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>]’
224 | operator<<(float __f)
| ^~~~~~~~
/usr/include/c++/10/ostream:224:24: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘float’
224 | operator<<(float __f)
| ~~~~~~^~~
/usr/include/c++/10/ostream:232:7: note: candidate: ‘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>]’
232 | operator<<(long double __f)
| ^~~~~~~~
/usr/include/c++/10/ostream:232:30: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘long double’
232 | operator<<(long double __f)
| ~~~~~~~~~~~~^~~
/usr/include/c++/10/ostream:245:7: note: candidate: ‘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>]’
245 | operator<<(const void* __p)
| ^~~~~~~~
/usr/include/c++/10/ostream:245:30: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘const void*’
245 | operator<<(const void* __p)
| ~~~~~~~~~~~~^~~
/usr/include/c++/10/ostream:250:7: note: candidate: ‘std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::nullptr_t) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::nullptr_t = std::nullptr_t]’
250 | operator<<(nullptr_t)
| ^~~~~~~~
/usr/include/c++/10/ostream:250:18: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘std::nullptr_t’
250 | operator<<(nullptr_t)
| ^~~~~~~~~
In file included from /usr/include/c++/10/ostream:784,
from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/bits/ostream.tcc:119:5: note: candidate: ‘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>]’
119 | basic_ostream<_CharT, _Traits>::
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/10/bits/ostream.tcc:120:34: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘std::basic_ostream<char>::__streambuf_type*’ {aka ‘std::basic_streambuf<char>*’}
120 | operator<<(__streambuf_type* __sbin)
| ~~~~~~~~~~~~~~~~~~^~~~~~
ostest.cpp:66:17: note: candidate: ‘std::ostream& N::operator<<(std::ostream&, const N::A&)’
66 | ::std::ostream& operator<<(::std::ostream& out, const A& a)
| ^~~~~~~~
ostest.cpp:66:58: note: no known conversion for argument 2 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘const N::A&’
66 | ::std::ostream& operator<<(::std::ostream& out, const A& a)
| ~~~~~~~~~^
In file included from /usr/include/c++/10/bits/basic_string.h:48,
from /usr/include/c++/10/string:55,
from /usr/include/c++/10/bits/locale_classes.h:40,
from /usr/include/c++/10/bits/ios_base.h:41,
from /usr/include/c++/10/ios:42,
from /usr/include/c++/10/ostream:38,
from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/string_view:619:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::basic_string_view<_CharT, _Traits>)’
619 | operator<<(basic_ostream<_CharT, _Traits>& __os,
| ^~~~~~~~
/usr/include/c++/10/string_view:619:5: note: template argument deduction/substitution failed:
ostest.cpp:68:48: note: ‘std::vector<unsigned int>’ is not derived from ‘std::basic_string_view<_CharT, _Traits>’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ^
In file included from /usr/include/c++/10/string:55,
from /usr/include/c++/10/bits/locale_classes.h:40,
from /usr/include/c++/10/bits/ios_base.h:41,
from /usr/include/c++/10/ios:42,
from /usr/include/c++/10/ostream:38,
from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/bits/basic_string.h:6468:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&)’
6468 | operator<<(basic_ostream<_CharT, _Traits>& __os,
| ^~~~~~~~
/usr/include/c++/10/bits/basic_string.h:6468:5: note: template argument deduction/substitution failed:
ostest.cpp:68:48: note: ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ^
In file included from /usr/include/c++/10/bits/ios_base.h:46,
from /usr/include/c++/10/ios:42,
from /usr/include/c++/10/ostream:38,
from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/system_error:262:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::error_code&)’
262 | operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
| ^~~~~~~~
/usr/include/c++/10/system_error:262:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘const std::error_code&’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:506:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)’
506 | operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
| ^~~~~~~~
/usr/include/c++/10/ostream:506:5: note: template argument deduction/substitution failed:
ostest.cpp:68:48: note: deduced conflicting types for parameter ‘_CharT’ (‘char’ and ‘std::vector<unsigned int>’)
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ^
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:511:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)’
511 | operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
| ^~~~~~~~
/usr/include/c++/10/ostream:511:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘char’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:517:5: note: candidate: ‘template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)’
517 | operator<<(basic_ostream<char, _Traits>& __out, char __c)
| ^~~~~~~~
/usr/include/c++/10/ostream:517:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘char’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:523:5: note: candidate: ‘template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)’
523 | operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
| ^~~~~~~~
/usr/include/c++/10/ostream:523:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘signed char’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:528:5: note: candidate: ‘template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)’
528 | operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
| ^~~~~~~~
/usr/include/c++/10/ostream:528:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘unsigned char’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:589:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)’
589 | operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
| ^~~~~~~~
/usr/include/c++/10/ostream:589:5: note: template argument deduction/substitution failed:
ostest.cpp:68:48: note: mismatched types ‘const _CharT*’ and ‘std::vector<unsigned int>’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ^
In file included from /usr/include/c++/10/ostream:784,
from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/bits/ostream.tcc:321:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)’
321 | operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
| ^~~~~~~~
/usr/include/c++/10/bits/ostream.tcc:321:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘const char*’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:606:5: note: candidate: ‘template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)’
606 | operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
| ^~~~~~~~
/usr/include/c++/10/ostream:606:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘const char*’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:619:5: note: candidate: ‘template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)’
619 | operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
| ^~~~~~~~
/usr/include/c++/10/ostream:619:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘const signed char*’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:624:5: note: candidate: ‘template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)’
624 | operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
| ^~~~~~~~
/usr/include/c++/10/ostream:624:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘const unsigned char*’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:773:5: note: candidate: ‘template<class _Ostream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_ostream<_Ostream>, std::__is_insertable<typename std::__is_convertible_to_basic_ostream<_Tp>::__ostream_type, const _Tp&, void> >::value, typename std::__is_convertible_to_basic_ostream<_Tp>::__ostream_type>::type std::operator<<(_Ostream&&, const _Tp&)’
773 | operator<<(_Ostream&& __os, const _Tp& __x)
| ^~~~~~~~
[snip]
好的,所以我不能像这样专门化向量。让我们这样做:
struct printable_UV
{
printable_UV(const UV& uv) : ref(uv) {}
const UV& ref;
};
::std::ostream& operator<<(::std::ostream& out, const printable_UV& uv)
{
out << "(";
::std::copy(uv.ref.begin(), ::std::prev(uv.ref.end()),
::std::ostream_iterator<unsigned int>(out, ", "));
out << uv.ref.back();
out << ")";
return out;
}
啪嗒啪嗒的声音仍然存在。但是,如果我使用一个没有这样的名称空间的更简单的示例:
#include <iostream>
#include <vector>
#include <iterator>
using vec = ::std::vector<unsigned int>;
struct printable
{
const vec& ref;
printable(const vec& v) : ref(v) {}
};
::std::ostream& operator<<(::std::ostream& out, const printable& v)
{
out << "(";
::std::copy(v.ref.begin(), ::std::prev(v.ref.end()),
::std::ostream_iterator<unsigned int>(out, ", "));
out << v.ref.back();
out << ")";
return out;
}
int main(int argc, char **argv)
{
vec v = { 1, 2, 3 };
::std::cout << v << ::std::endl;
return 0;
}
它工作得很好。这让我认为引入命名空间会使查找失败。我做错了什么?
First of all: I'm not an expert in C++ but I'm writing C++ bindings for a low-level C library and need some help to take the correct approach.
Below is the problem I'm facing abstracted into a simple example (see comments):
#include <iostream>
#include <vector>
#include <iterator>
#include <map>
// We have a namespace N.
namespace N {
// And a nested namespace NN.
namespace NN {
// A scoped enum E.
enum class E {
FIRST = 1,
SECOND,
};
// And a typedef for a vector holding E values.
using EV = ::std::vector<E>;
// We want to make E printable so let's provide a mapping of values
// to human-readable names ...
const ::std::map<E, ::std::string> E_names = {
{ E::FIRST, "FIRST" },
{ E::SECOND, "SECOND" }
};
// ... and stream insertion operator for the enum ...
::std::ostream& operator<<(::std::ostream& out, E e)
{
out << E_names.at(e);
return out;
}
// ... and for the vector.
::std::ostream& operator<<(::std::ostream& out, const EV& ev)
{
out << "(";
::std::copy(ev.begin(), ::std::prev(ev.end()),
::std::ostream_iterator<E>(out, ", "));
out << ev.back();
out << ")";
return out;
}
} /* namespace NN */
// Now back in namespace N, let's define the following class:
class A
{
public:
explicit A(void) {}
void m(const NN::EV& ev) {}
NN::EV get_ev(void) const { return NN::EV({ NN::E::FIRST, NN::E::SECOND}); }
};
// And its own stream insertion operator:
::std::ostream& operator<<(::std::ostream& out, const A& a)
{
out << "A(" << a.get_ev() << ")";
return out;
}
} /* namespace N */
// And do the following in main():
int main(int argc, char **argv)
{
::std::cout << N::NN::E::FIRST << ::std::endl;
::std::cout << N::NN::EV({ N::NN::E::FIRST, N::NN::E::SECOND }) << ::std::endl;
::std::cout << N::A().get_ev() << ::std::endl;
::std::cout << N::A() << ::std::endl;
return 0;
}
The above works fine. Now let's extend the example as follows:
#include <iostream>
#include <vector>
#include <iterator>
#include <map>
namespace N {
namespace NN {
enum class E {
FIRST = 1,
SECOND,
};
using EV = ::std::vector<E>;
// Let's add another typedef for a vector holding unsigned integers.
using UV = ::std::vector<unsigned int>;
const ::std::map<E, ::std::string> E_names = {
{ E::FIRST, "FIRST" },
{ E::SECOND, "SECOND" }
};
::std::ostream& operator<<(::std::ostream& out, E e)
{
out << E_names.at(e);
return out;
}
::std::ostream& operator<<(::std::ostream& out, const EV& ev)
{
out << "(";
::std::copy(ev.begin(), ::std::prev(ev.end()),
::std::ostream_iterator<E>(out, ", "));
out << ev.back();
out << ")";
return out;
}
// And define the following stream insertion operator.
::std::ostream& operator<<(::std::ostream& out, const UV& uv)
{
out << "(";
::std::copy(uv.begin(), ::std::prev(uv.end()),
::std::ostream_iterator<unsigned int>(out, ", "));
out << uv.back();
out << ")";
return out;
}
} /* namespace NN */
class A
{
public:
explicit A(void) {}
void m(const NN::EV& ev) {}
NN::EV get_ev(void) const { return NN::EV({ NN::E::FIRST, NN::E::SECOND}); }
NN::UV get_uv(void) const { return NN::UV({ 10, 20 }); }
};
::std::ostream& operator<<(::std::ostream& out, const A& a)
{
out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
return out;
}
} /* namespace N */
int main(int argc, char **argv)
{
::std::cout << N::NN::E::FIRST << ::std::endl;
::std::cout << N::NN::EV({ N::NN::E::FIRST, N::NN::E::SECOND }) << ::std::endl;
::std::cout << N::A().get_ev() << ::std::endl;
::std::cout << N::A().get_uv() << ::std::endl;
::std::cout << N::A() << ::std::endl;
return 0;
}
Now the following splat is generated by gcc:
ostest.cpp: In function ‘std::ostream& N::operator<<(std::ostream&, const N::A&)’:
ostest.cpp:68:36: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’})
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^~ ~~~~~~~~~~
| | |
| | N::NN::UV {aka std::vector<unsigned int>}
| std::basic_ostream<char>
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:108:7: note: candidate: ‘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>]’
108 | operator<<(__ostream_type& (*__pf)(__ostream_type&))
| ^~~~~~~~
/usr/include/c++/10/ostream:108:36: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&)’ {aka ‘std::basic_ostream<char>& (*)(std::basic_ostream<char>&)’}
108 | operator<<(__ostream_type& (*__pf)(__ostream_type&))
| ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/10/ostream:117:7: note: candidate: ‘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>]’
117 | operator<<(__ios_type& (*__pf)(__ios_type&))
| ^~~~~~~~
/usr/include/c++/10/ostream:117:32: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&)’ {aka ‘std::basic_ios<char>& (*)(std::basic_ios<char>&)’}
117 | operator<<(__ios_type& (*__pf)(__ios_type&))
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
/usr/include/c++/10/ostream:127:7: note: candidate: ‘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>]’
127 | operator<<(ios_base& (*__pf) (ios_base&))
| ^~~~~~~~
/usr/include/c++/10/ostream:127:30: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘std::ios_base& (*)(std::ios_base&)’
127 | operator<<(ios_base& (*__pf) (ios_base&))
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/10/ostream:166:7: note: candidate: ‘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>]’
166 | operator<<(long __n)
| ^~~~~~~~
/usr/include/c++/10/ostream:166:23: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘long int’
166 | operator<<(long __n)
| ~~~~~^~~
/usr/include/c++/10/ostream:170:7: note: candidate: ‘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>]’
170 | operator<<(unsigned long __n)
| ^~~~~~~~
/usr/include/c++/10/ostream:170:32: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘long unsigned int’
170 | operator<<(unsigned long __n)
| ~~~~~~~~~~~~~~^~~
/usr/include/c++/10/ostream:174:7: note: candidate: ‘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>]’
174 | operator<<(bool __n)
| ^~~~~~~~
/usr/include/c++/10/ostream:174:23: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘bool’
174 | operator<<(bool __n)
| ~~~~~^~~
In file included from /usr/include/c++/10/ostream:784,
from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/bits/ostream.tcc:91:5: note: candidate: ‘std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char; _Traits = std::char_traits<char>]’
91 | basic_ostream<_CharT, _Traits>::
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/10/bits/ostream.tcc:92:22: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘short int’
92 | operator<<(short __n)
| ~~~~~~^~~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:181:7: note: candidate: ‘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>]’
181 | operator<<(unsigned short __n)
| ^~~~~~~~
/usr/include/c++/10/ostream:181:33: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘short unsigned int’
181 | operator<<(unsigned short __n)
| ~~~~~~~~~~~~~~~^~~
In file included from /usr/include/c++/10/ostream:784,
from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/bits/ostream.tcc:105:5: note: candidate: ‘std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char; _Traits = std::char_traits<char>]’
105 | basic_ostream<_CharT, _Traits>::
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/10/bits/ostream.tcc:106:20: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘int’
106 | operator<<(int __n)
| ~~~~^~~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:192:7: note: candidate: ‘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>]’
192 | operator<<(unsigned int __n)
| ^~~~~~~~
/usr/include/c++/10/ostream:192:31: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘unsigned int’
192 | operator<<(unsigned int __n)
| ~~~~~~~~~~~~~^~~
/usr/include/c++/10/ostream:201:7: note: candidate: ‘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>]’
201 | operator<<(long long __n)
| ^~~~~~~~
/usr/include/c++/10/ostream:201:28: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘long long int’
201 | operator<<(long long __n)
| ~~~~~~~~~~^~~
/usr/include/c++/10/ostream:205:7: note: candidate: ‘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>]’
205 | operator<<(unsigned long long __n)
| ^~~~~~~~
/usr/include/c++/10/ostream:205:37: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘long long unsigned int’
205 | operator<<(unsigned long long __n)
| ~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/10/ostream:220:7: note: candidate: ‘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>]’
220 | operator<<(double __f)
| ^~~~~~~~
/usr/include/c++/10/ostream:220:25: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘double’
220 | operator<<(double __f)
| ~~~~~~~^~~
/usr/include/c++/10/ostream:224:7: note: candidate: ‘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>]’
224 | operator<<(float __f)
| ^~~~~~~~
/usr/include/c++/10/ostream:224:24: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘float’
224 | operator<<(float __f)
| ~~~~~~^~~
/usr/include/c++/10/ostream:232:7: note: candidate: ‘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>]’
232 | operator<<(long double __f)
| ^~~~~~~~
/usr/include/c++/10/ostream:232:30: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘long double’
232 | operator<<(long double __f)
| ~~~~~~~~~~~~^~~
/usr/include/c++/10/ostream:245:7: note: candidate: ‘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>]’
245 | operator<<(const void* __p)
| ^~~~~~~~
/usr/include/c++/10/ostream:245:30: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘const void*’
245 | operator<<(const void* __p)
| ~~~~~~~~~~~~^~~
/usr/include/c++/10/ostream:250:7: note: candidate: ‘std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::nullptr_t) [with _CharT = char; _Traits = std::char_traits<char>; std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>; std::nullptr_t = std::nullptr_t]’
250 | operator<<(nullptr_t)
| ^~~~~~~~
/usr/include/c++/10/ostream:250:18: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘std::nullptr_t’
250 | operator<<(nullptr_t)
| ^~~~~~~~~
In file included from /usr/include/c++/10/ostream:784,
from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/bits/ostream.tcc:119:5: note: candidate: ‘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>]’
119 | basic_ostream<_CharT, _Traits>::
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/10/bits/ostream.tcc:120:34: note: no known conversion for argument 1 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘std::basic_ostream<char>::__streambuf_type*’ {aka ‘std::basic_streambuf<char>*’}
120 | operator<<(__streambuf_type* __sbin)
| ~~~~~~~~~~~~~~~~~~^~~~~~
ostest.cpp:66:17: note: candidate: ‘std::ostream& N::operator<<(std::ostream&, const N::A&)’
66 | ::std::ostream& operator<<(::std::ostream& out, const A& a)
| ^~~~~~~~
ostest.cpp:66:58: note: no known conversion for argument 2 from ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} to ‘const N::A&’
66 | ::std::ostream& operator<<(::std::ostream& out, const A& a)
| ~~~~~~~~~^
In file included from /usr/include/c++/10/bits/basic_string.h:48,
from /usr/include/c++/10/string:55,
from /usr/include/c++/10/bits/locale_classes.h:40,
from /usr/include/c++/10/bits/ios_base.h:41,
from /usr/include/c++/10/ios:42,
from /usr/include/c++/10/ostream:38,
from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/string_view:619:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, std::basic_string_view<_CharT, _Traits>)’
619 | operator<<(basic_ostream<_CharT, _Traits>& __os,
| ^~~~~~~~
/usr/include/c++/10/string_view:619:5: note: template argument deduction/substitution failed:
ostest.cpp:68:48: note: ‘std::vector<unsigned int>’ is not derived from ‘std::basic_string_view<_CharT, _Traits>’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ^
In file included from /usr/include/c++/10/string:55,
from /usr/include/c++/10/bits/locale_classes.h:40,
from /usr/include/c++/10/bits/ios_base.h:41,
from /usr/include/c++/10/ios:42,
from /usr/include/c++/10/ostream:38,
from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/bits/basic_string.h:6468:5: note: candidate: ‘template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>&)’
6468 | operator<<(basic_ostream<_CharT, _Traits>& __os,
| ^~~~~~~~
/usr/include/c++/10/bits/basic_string.h:6468:5: note: template argument deduction/substitution failed:
ostest.cpp:68:48: note: ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’} is not derived from ‘const std::__cxx11::basic_string<_CharT, _Traits, _Allocator>’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ^
In file included from /usr/include/c++/10/bits/ios_base.h:46,
from /usr/include/c++/10/ios:42,
from /usr/include/c++/10/ostream:38,
from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/system_error:262:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::error_code&)’
262 | operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __e)
| ^~~~~~~~
/usr/include/c++/10/system_error:262:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘const std::error_code&’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:506:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)’
506 | operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
| ^~~~~~~~
/usr/include/c++/10/ostream:506:5: note: template argument deduction/substitution failed:
ostest.cpp:68:48: note: deduced conflicting types for parameter ‘_CharT’ (‘char’ and ‘std::vector<unsigned int>’)
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ^
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:511:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)’
511 | operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
| ^~~~~~~~
/usr/include/c++/10/ostream:511:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘char’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:517:5: note: candidate: ‘template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)’
517 | operator<<(basic_ostream<char, _Traits>& __out, char __c)
| ^~~~~~~~
/usr/include/c++/10/ostream:517:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘char’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:523:5: note: candidate: ‘template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)’
523 | operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
| ^~~~~~~~
/usr/include/c++/10/ostream:523:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘signed char’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:528:5: note: candidate: ‘template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)’
528 | operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
| ^~~~~~~~
/usr/include/c++/10/ostream:528:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘unsigned char’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:589:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)’
589 | operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
| ^~~~~~~~
/usr/include/c++/10/ostream:589:5: note: template argument deduction/substitution failed:
ostest.cpp:68:48: note: mismatched types ‘const _CharT*’ and ‘std::vector<unsigned int>’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ^
In file included from /usr/include/c++/10/ostream:784,
from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/bits/ostream.tcc:321:5: note: candidate: ‘template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)’
321 | operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
| ^~~~~~~~
/usr/include/c++/10/bits/ostream.tcc:321:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘const char*’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:606:5: note: candidate: ‘template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)’
606 | operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
| ^~~~~~~~
/usr/include/c++/10/ostream:606:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘const char*’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:619:5: note: candidate: ‘template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)’
619 | operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
| ^~~~~~~~
/usr/include/c++/10/ostream:619:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘const signed char*’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:624:5: note: candidate: ‘template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)’
624 | operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
| ^~~~~~~~
/usr/include/c++/10/ostream:624:5: note: template argument deduction/substitution failed:
ostest.cpp:68:47: note: cannot convert ‘N::A::get_uv() const()’ (type ‘N::NN::UV’ {aka ‘std::vector<unsigned int>’}) to type ‘const unsigned char*’
68 | out << "A(" << a.get_ev() << ", " << a.get_uv() << ")";
| ~~~~~~~~^~
In file included from /usr/include/c++/10/iostream:39,
from ostest.cpp:1:
/usr/include/c++/10/ostream:773:5: note: candidate: ‘template<class _Ostream, class _Tp> typename std::enable_if<std::__and_<std::__not_<std::is_lvalue_reference<_Tp> >, std::__is_convertible_to_basic_ostream<_Ostream>, std::__is_insertable<typename std::__is_convertible_to_basic_ostream<_Tp>::__ostream_type, const _Tp&, void> >::value, typename std::__is_convertible_to_basic_ostream<_Tp>::__ostream_type>::type std::operator<<(_Ostream&&, const _Tp&)’
773 | operator<<(_Ostream&& __os, const _Tp& __x)
| ^~~~~~~~
[snip]
Ok so I can't specialize vector like that. Let's do this:
struct printable_UV
{
printable_UV(const UV& uv) : ref(uv) {}
const UV& ref;
};
::std::ostream& operator<<(::std::ostream& out, const printable_UV& uv)
{
out << "(";
::std::copy(uv.ref.begin(), ::std::prev(uv.ref.end()),
::std::ostream_iterator<unsigned int>(out, ", "));
out << uv.ref.back();
out << ")";
return out;
}
The splat is still there. But if I go with a simpler example with no namespaces like this:
#include <iostream>
#include <vector>
#include <iterator>
using vec = ::std::vector<unsigned int>;
struct printable
{
const vec& ref;
printable(const vec& v) : ref(v) {}
};
::std::ostream& operator<<(::std::ostream& out, const printable& v)
{
out << "(";
::std::copy(v.ref.begin(), ::std::prev(v.ref.end()),
::std::ostream_iterator<unsigned int>(out, ", "));
out << v.ref.back();
out << ")";
return out;
}
int main(int argc, char **argv)
{
vec v = { 1, 2, 3 };
::std::cout << v << ::std::endl;
return 0;
}
It works just fine. This makes me think that introducing namespaces makes the lookup fail. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论