萨特书中的错误? [英] Error in Sutter book?

查看:38
本文介绍了萨特书中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更多例外C ++书中的第1项使用以下结构:


fstream in;


....


进程(in.is_open()?in:cin,...);

其中进程显示为具有多种不同的

可能的定义。其中包括:


void进程(basic_istream< char>& in,..);


我尝试时遇到错误在visual studio中这样做。对我来说,它看起来像是一个错误的构造(使用带有两个

不同类型的三元传回)所以我想验证它

实际上工作了,但事实并非如此。我也尝试过这样的事情:


istream& s = in.is_open()? in:cin;


没有去。


使用指针确实有效:


istream * s = in.is_open()? & in:& cin;


这不是:


istream * s =&(in.is_open()?in :cin);


编译器在这里打破标准还是使用三元

运算符有缺陷?


他的另一个定义是:


模板< typename In,typename Out>

void process(In& in,Out& out)

{

....

}


但是,如何使用三元运算符:


process(in.is_open( )?in:cin,...)
我认为编译器可能非常聪明并且基于istream而不是fstream或者派生模板

无论是什么,但是我不会认为有这样的事情,是吗?或者它可以创建两个

的进程实例并创建一个完全不同的分支

构造...再次,我不知道任何会发生的事情

在符合条件的情况下好像。


但如果我是对的,这怎么会成为如此高调的书呢? />
由如此高调的作者...所以我质疑我的理解。

解决方案



Noah Roberts写道:


istream& s = in.is_open()? in:cin;



我认为收到的错误可能是有益的。

在我的操作中使用三元

运算符的所有用途(除了一个指针使用之外)都是一样的。

1> c:\program files\microsoft visual studio 8 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ '':无法访问

在类'std :: basic_ios< _Elem,_Traits>'中声明的私有成员'

1 with

1 [

1 _Elem = char,

1 _Traits = std :: char_traits< char>

1]

1 c:\ program Files \ microsoft visual studio

8 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ basic_ios< _Elem,_Traits> :: basic_ios''

1 with

1 [

1 _Elem = char,

1 _Traits = std :: char_traits< char>

1]

1发生此诊断在编译器生成的函数中

''std :: basic_istream< _Elem,_Traits> :: basic_istream(const

std :: basic_istream< _Elem,_Traits&)''

1与

1 [

1 _Elem = char,

1 _Traits = std :: char_traits< char>

1]


Noah Roberts写道:


Item更多例外C ++书中的#1使用以下结构:


fstream in;


...

流程(in.is_open()? in:cin,...);


其中,过程显示为具有多种不同的可能定义。其中包括:


void进程(basic_istream< char>& in,..);


我尝试时遇到错误在visual studio中这样做。



GCC和Comeau都适合我。


* Noah Roberts:
< blockquote class =post_quotes>
更多例外C ++书中的第1项使用以下结构:


fstream in;


...


流程(in.is_open()?in:cin,...);


流程在哪里显示为具有多种不同的可能定义。其中包括:


void进程(basic_istream< char>& in,..);


我尝试时遇到错误在visual studio中这样做。对我来说,它看起来像是一个错误的构造(使用带有两个

不同类型的三元传回)所以我想验证它

实际上工作了,但事实并非如此。我也尝试过这样的事情:


istream& s = in.is_open()? in:cin;


没有去。


使用指针确实有效:


istream * s = in.is_open()? & in:& cin;


这不是:


istream * s =&(in.is_open()?in :cin);


编译器在这里打破标准还是使用三元

运算符有缺陷?



取决于''in''和'cin''的类型是否相同。如果它们是,并且

都是左值,则结果是左值。否则结果是一个

rvalue,这意味着复制,这对于流是没有意义的。

[snip]


但是,如果我是对的,这是如何成为如此高调的作者如此高调的一本书......所以我质疑我的理解。



查看错误页面。也许有一些东西。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


Item #1 in the More Exceptional C++ book uses the following construct:

fstream in;

....

process( in.is_open() ? in : cin,...);
Where process has been shown as having various multiple different
possible definitions. Among them is:

void process(basic_istream<char>& in,..);

I get errors when I try to do this in visual studio. It looked like an
erroneous construct to me to begin with (using ternary with two
different types being passed back) so I wanted to verify that it
actually worked and it doesn''t. I also tried something like so:

istream & s = in.is_open() ? in : cin;

no go.

Working with pointers does work:

istream * s = in.is_open() ? &in : &cin;

This does not:

istream * s = &(in.is_open() ? in : cin);

Is the compiler breaking the standard here or is the use of the ternary
operator flawed?

Another definition he has is:

template < typename In, typename Out>
void process(In & in, Out & out)
{
....
}

But again, how can the ternary operator be used as such:

process(in.is_open() ? in : cin, ...)

I suppose the compiler could be really smart and derive a template
based on istream instead of either fstream or whatever cin is but I
don''t think there is such a thing, is there? Or it could create two
instances of process and create a totally different branching
construct...again, I don''t know of any that would and that seems on the
outside of qualifying for "as if".

But if I am right, how did this make it into such a high profile book
by such a high profile author...so I question my understanding.

解决方案


Noah Roberts wrote:

istream & s = in.is_open() ? in : cin;

I suppose the error recieved might be of benefit. It is the same for
all uses (except the one pointer use that works) of the ternary
operator in my op.
1>c:\program files\microsoft visual studio 8\vc\include\istream(842) :
error C2248: ''std::basic_ios<_Elem,_Traits>::basic_ios'' : cannot access
private member declared in class ''std::basic_ios<_Elem,_Traits>''
1 with
1 [
1 _Elem=char,
1 _Traits=std::char_traits<char>
1 ]
1 c:\program files\microsoft visual studio
8\vc\include\ios(151) : see declaration of
''std::basic_ios<_Elem,_Traits>::basic_ios''
1 with
1 [
1 _Elem=char,
1 _Traits=std::char_traits<char>
1 ]
1 This diagnostic occurred in the compiler generated function
''std::basic_istream<_Elem,_Traits>::basic_istream( const
std::basic_istream<_Elem,_Traits&)''
1 with
1 [
1 _Elem=char,
1 _Traits=std::char_traits<char>
1 ]


Noah Roberts wrote:

Item #1 in the More Exceptional C++ book uses the following construct:

fstream in;

...

process( in.is_open() ? in : cin,...);
Where process has been shown as having various multiple different
possible definitions. Among them is:

void process(basic_istream<char>& in,..);

I get errors when I try to do this in visual studio.

Works fine for me with both GCC and Comeau.


* Noah Roberts:

Item #1 in the More Exceptional C++ book uses the following construct:

fstream in;

...

process( in.is_open() ? in : cin,...);
Where process has been shown as having various multiple different
possible definitions. Among them is:

void process(basic_istream<char>& in,..);

I get errors when I try to do this in visual studio. It looked like an
erroneous construct to me to begin with (using ternary with two
different types being passed back) so I wanted to verify that it
actually worked and it doesn''t. I also tried something like so:

istream & s = in.is_open() ? in : cin;

no go.

Working with pointers does work:

istream * s = in.is_open() ? &in : &cin;

This does not:

istream * s = &(in.is_open() ? in : cin);

Is the compiler breaking the standard here or is the use of the ternary
operator flawed?

Depends whether ''in'' and ''cin'' are of identical type. If they are, and
both are lvalues, the result is an lvalue. Otherwise the result is an
rvalue, which implies copying, which is meaningless for streams.
[snip]

But if I am right, how did this make it into such a high profile book
by such a high profile author...so I question my understanding.

Check the errate pages. Perhaps there''s something.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于萨特书中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆