N
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?
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.
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.