简体   繁体   English

std :: setw,std :: setfill等的实际返回类型是什么?

[英]what is the real return type of std::setw, std::setfill and so on?

I'm curious of the real return type of std::setw and std::setfill 我很好奇std :: setw和std :: setfill的真正返回类型

As we see references, their datatype of return value is "undefined". 如我们所见,引用的返回值的数据类型为“未定义”。 However, Is it possible to declare a function without return type? 但是,是否可以声明没有返回类型的函数?

In a situation that I have to develop a method supplying extended features for 'cout' or 'cin', and this method should be called like 在这种情况下,我必须开发一种为“ cout”或“ cin”提供扩展功能的方法,并且该方法应称为

cout << foo(32, 'A', 0.00f) << "Hello world!";

How should I declare the method? 我应该如何声明该方法?

The return type of std::setw et al is unspecified because each C++ implementation may decide to do it differently, so there's no one answer - you have to survey the compilers/versions you're interested in. 未指定std::setw等的返回类型,因为每种C ++实现可能会决定以不同的方式进行操作,因此没有答案-您必须调查您感兴趣的编译器/版本。

Looking at the libstdc++ used with GCC, we see: 查看与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 is a small struct to capture the width parameter, and which std::ostream& operator<<(std::ostream&, _Setw) and ...>>... can then handle to set the width in the stream: _Setw是捕获width参数的一个小结构,并且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     }

As we see references, their datatype of return value is "undefined". 如我们所见,引用的返回值的数据类型为“未定义”。

It's unspecified , not undefined. 它是未指定的 ,不是未定义的。

However, Is it possible to declare a function without return type? 但是,是否可以声明没有返回类型的函数?

No - every function must have a return type, even if only void . 否-每个函数都必须具有返回类型,即使只有void

In a situation that I have to develop a method supplying extended features for 'cout' or 'cin', and this method should be called like 在这种情况下,我必须开发一种为“ cout”或“ cin”提供扩展功能的方法,并且该方法应称为

cout << foo(32, 'A', 0.00f) << "Hello world!";

How should I declare the method? 我应该如何声明该方法?

You can do something similar and have function foo return an object that you write streaming operators for. 您可以执行类似的操作,并让函数foo返回为其编写流运算符的对象。 Those streaming functions should then manipulate the stream: you'll need to use iword and xalloc to give the streams extra state to track the potentially modified behaviours you're going to add - see this answer . 然后,这些流函数应该操纵流:您将需要使用iwordxalloc为流提供额外的状态,以跟踪要添加的可能修改的行为-请参见此答案

It is unspecified, or in other words, not specified in the C++ standard and depends on your library implementation. 它是未指定的,换句话说,未在C ++标准中指定,并且取决于您的库实现。 More on that here . 这里更多。

You can't declare your own function to manipulate the insertion/extraction operators unless you overload them to accept a new type. 您不能声明自己的函数来操作插入/提取运算符,除非您使它们重载以接受新类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM