std::setw et al 的返回类型未指定,因为每个 C++ 实现可能会决定以不同的方式执行此操作,因此没有一个答案 - 您必须调查您感兴趣的编译器/版本。
查看与 GCC 一起使用的 libstdc++,我们看到:
00214 struct _Setw { int _M_n; };
00215
00216 /**
00217 * @brief Manipulator for @c width.
00218 * @param n The new width.
00219 *
00220 * Sent to a stream object, this manipulator calls @c width(n) for
00221 * that object.
00222 */
00223 inline _Setw
00224 setw(int __n)
00225 { return { __n }; }
_Setw 是一个用于捕获宽度参数的小结构,然后std::ostream& operator<<(std::ostream&, _Setw) 和...>>... 可以处理它来设置流中的宽度:
00227 template<typename _CharT, typename _Traits>
00228 inline basic_istream<_CharT, _Traits>&
00229 operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f)
00230 {
00231 __is.width(__f._M_n);
00232 return __is;
00233 }
00234
00235 template<typename _CharT, typename _Traits>
00236 inline basic_ostream<_CharT, _Traits>&
00237 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
00238 {
00239 __os.width(__f._M_n);
00240 return __os;
00241 }
正如我们看到的引用,它们的返回值数据类型是“未定义”。
它是未指定,而不是未定义。
但是,是否可以声明一个没有返回类型的函数?
否 - 每个函数都必须有一个返回类型,即使只有 void。
在我必须开发一种为“cout”或“cin”提供扩展功能的方法的情况下,应该像这样调用此方法
cout << foo(32, 'A', 0.00f) << "Hello world!";
我应该如何声明方法?
您可以做类似的事情,让函数foo 返回一个您为其编写流式操作符的对象。然后,这些流函数应该操纵流:您需要使用 iword 和 xalloc 为流提供额外的状态,以跟踪您要添加的可能修改的行为 - 请参阅 this answer。