 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mathias Gaunard Guest
|
Posted: Fri Apr 13, 2007 1:21 am Post subject: 'anystate' qualifier |
|
|
Here is a crazy idea that I just came up with.
I was thinking of the issue caused by the unneeded temporaries that
happened when returning std::string by value.
std::string foo()
{
std::string s;
/* some work on s */
return s;
}
std::string s = foo(); // with a smart compiler doing NRVO, no
unneeded copy, construction or destruction
s = foo(); // constructs a tempory, calls s.operator=, then destructs
the temporary
A possible solution would simply to write the function that way so
that another string could be reused:
void foo(std::string& s)
{
s = ""; // to make it empty and forget about the old contents
/* some work on s */
}
My crazy idea was to simply allow code with the first signature to do
similar thing as the second one.
std::string foo()
{
anystate std::string s;
s = ""; // to make it empty and forget about the old contents
/* some work on s */
return s;
}
Basically, 'anystate' means that the object can be in any valid state
(after a construction and before a destruction).
With that indication, the compiler would be able to reuse the return
value in the function.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Nevin :-] Liber Guest
|
Posted: Fri Apr 13, 2007 8:55 am Post subject: Re: 'anystate' qualifier |
|
|
In article <1176422460.558589.322120 (AT) y80g2000hsf (DOT) googlegroups.com>,
"Mathias Gaunard" <loufoque (AT) gmail (DOT) com> wrote:
| Quote: | void foo(std::string& s)
{
s = ""; // to make it empty and forget about the old contents
/* some work on s */
}
My crazy idea was to simply allow code with the first signature to do
similar thing as the second one.
std::string foo()
{
anystate std::string s;
s = ""; // to make it empty and forget about the old contents
/* some work on s */
return s;
}
|
They are not semantically equivalent. Take the case where something
inside of foo throws.
--
Nevin " " Liber <mailto:nevin (AT) eviloverlord (DOT) com> 773 961-1620
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Fri Apr 13, 2007 7:05 pm Post subject: Re: 'anystate' qualifier |
|
|
Mathias Gaunard ha scritto:
| Quote: |
void foo(std::string& s)
{
s = ""; // to make it empty and forget about the old contents
|
It's much better to use s.clear() for that.
| Quote: | /* some work on s */
}
My crazy idea was to simply allow code with the first signature to do
similar thing as the second one.
std::string foo()
{
anystate std::string s;
s = ""; // to make it empty and forget about the old contents
/* some work on s */
return s;
}
Basically, 'anystate' means that the object can be in any valid state
(after a construction and before a destruction).
|
I don't get it. Of course any object can be in "any valid state". How
can it be otherwise? Why should I tell the compiler about it?
| Quote: | With that indication, the compiler would be able to reuse the return
value in the function.
|
It not clear to me what you mean. Anyway, a quite good optimization of
this specific scenario can be achieved with rvalue references and move
semantic. When we will have them (and we definitely will) would your
idea still be useful?
Regards,
Ganesh
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Greg Herlihy Guest
|
Posted: Mon Apr 16, 2007 8:07 am Post subject: Re: 'anystate' qualifier |
|
|
On 4/12/07 6:21 PM, in article
1176422460.558589.322120 (AT) y80g2000hsf (DOT) googlegroups.com, "Mathias Gaunard"
<loufoque (AT) gmail (DOT) com> wrote:
| Quote: | Here is a crazy idea that I just came up with.
I was thinking of the issue caused by the unneeded temporaries that
happened when returning std::string by value.
std::string foo()
{
std::string s;
/* some work on s */
return s;
}
std::string s = foo(); // with a smart compiler doing NRVO, no
unneeded copy, construction or destruction
s = foo(); // constructs a tempory, calls s.operator=, then destructs
the temporary
|
The "named return value optimization" (NRVO) can eliminate the temporary in
the second example as well. Specifically, the NRVO optimization eliminates
the need for a temporary by extending the lifetime of the local variable
that foo() returns to be just as long as the lifetime of the temporary that
foo() would have ordinarily returned. In other words, the local variable
that foo() returns - and the temporary that holds foo()'s result (and whose
contents are copied into "s") - are one and the same.
| Quote: | A possible solution would simply to write the function that way so
that another string could be reused:
void foo(std::string& s)
{
s = ""; // to make it empty and forget about the old contents
/* some work on s */
}
|
This interface for foo() is inconvenient (and inefficient) whenever the
value to be transformed is either const or otherwise needs to be preserved.
A more convenient interface for foo() would separate input from output - and
would accept input by const reference as produce output by return value. And
thanks to the NRVO, the more convenient interface could well be the more
efficient interface as well.
Greg
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Mathias Gaunard Guest
|
Posted: Mon Apr 16, 2007 5:42 pm Post subject: Re: 'anystate' qualifier |
|
|
On 16 avr, 17:07, Greg Herlihy <gre...@pacbell.net> wrote:
| Quote: | The "named return value optimization" (NRVO) can eliminate the temporary in
the second example as well.
|
That was an example.
In a real case, the string is not simply default-constructed before,
it has any content. It can be in any state.
| Quote: | This interface for foo() is inconvenient (and inefficient) whenever the
value to be transformed is either const or otherwise needs to be preserved
A more convenient interface for foo() would separate input from output - and
would accept input by const reference as produce output by return value.
|
The whole point of the idea is to reuse a variable for the output
rather than constructing a new one.
| Quote: | And
thanks to the NRVO, the more convenient interface could well be the more
efficient interface as well.
|
It is not.
I will try to explain again, since you guys don't seem to get it.
std::string s = "bar";
s = foo();
Even if NRVO is applied, a new string will be default constructed in
foo(), then operator=() will be called to transfer its contents to s,
and finally that thing will be destructed.
There are obviously useless constructions/operator=/destructions here,
since we could simply reuse s.
It has been stated that with rvalue references, those operations will
become free. That is false, they will only become cheaper.
Rvalue-references only increase performance when free storage is used,
because you just need to swap pointers. But if automatic storage is
used instead (as it is with the famous small string optimization) then
the whole content would have to be copied anyway.
If we consider that the implementation use the small string
optimization, provide rvalue references and nrvo, then the data *will*
be copied in the previous example.
If the return value can be reused, however, there is no other copy
than the ones induced by initialization from string literals.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Mathias Gaunard Guest
|
Posted: Mon Apr 16, 2007 6:47 pm Post subject: Re: 'anystate' qualifier |
|
|
On 14 avr, 04:05, Alberto Ganesh Barbati <AlbertoBarb...@libero.it>
wrote:
| Quote: | I don't get it. Of course any object can be in "any valid state". How
can it be otherwise? Why should I tell the compiler about it?
|
Usually, when returning a default-constructible object, you construct
it, make your modifications, then return it.
With the anystate qualifier, you tell the compiler that there is no
need to have an object in the state "just default-constructed", it
could have been initialized a while before and have various operations
done on it. Of course, if no such object is available, the default-
constructor is used.
That simply allows the compiler to reuse the return value which
becomes your local variable, and if it's not possible it does create a
new one.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Tue Apr 17, 2007 6:43 pm Post subject: Re: 'anystate' qualifier |
|
|
Mathias Gaunard ha scritto:
| Quote: | On 14 avr, 04:05, Alberto Ganesh Barbati <AlbertoBarb...@libero.it
wrote:
I don't get it. Of course any object can be in "any valid state". How
can it be otherwise? Why should I tell the compiler about it?
Usually, when returning a default-constructible object, you construct
it, make your modifications, then return it.
With the anystate qualifier, you tell the compiler that there is no
need to have an object in the state "just default-constructed", it
could have been initialized a while before and have various operations
done on it. Of course, if no such object is available, the default-
constructor is used.
That simply allows the compiler to reuse the return value which
becomes your local variable, and if it's not possible it does create a
new one.
|
While I understand your concern, your syntax of your proposal is
inacceptable, in my opinion. Consider your example:
std::string s = "bar";
s = foo();
which, according to your original post, should be addressed with:
std::string foo()
{
anystate std::string s;
s = ""; // to make it empty and forget about the old contents
/* some work on s */
return s;
}
This syntax has the following inconveniences:
1) it's not clear at all that s is in fact an out *parameter* rather
than a regular local variable with some fancy attribute
2) it doesn't enforce the fact that s *must* be returned
3) it might let the programmer think it's possible to have more than one
"anystate" variable
4) the presence of the anystate variable is not exposed in the interface
(it's in the function definition but not in the declaration) so it might
not be available to the caller.
Point 4 is the most interesting. While it might be considered an
advantage (in the end, it should just be an implementation detail of
function foo), this makes it difficult if not impossible to implement
the feature! Yes, because the caller *must* pass in some way to foo()
some information about the need (or lack of) to construct the variable.
As the request of this information is not advertised in the declaration,
the compiler can't help but producing such information in *every*
function call. This is a two-fold problem: it violates the rule "don't
pay for what you don't need" and it will probably break all existing ABIs.
Just my opinion,
Ganesh
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
jose.diego@gmail.com Guest
|
Posted: Wed Apr 18, 2007 5:16 pm Post subject: Re: 'anystate' qualifier |
|
|
On Apr 16, 5:07 am, Greg Herlihy <gre...@pacbell.net> wrote:
| Quote: | On 4/12/07 6:21 PM, in article
1176422460.558589.322...@y80g2000hsf.googlegroups.com, "Mathias Gaunard"
loufo...@gmail.com> wrote:
Here is a crazy idea that I just came up with.
I was thinking of the issue caused by the unneeded temporaries that
happened when returning std::string by value.
std::string foo()
{
std::string s;
/* some work on s */
return s;
}
std::string s = foo(); // with a smart compiler doing NRVO, no
unneeded copy, construction or destruction
s = foo(); // constructs a tempory, calls s.operator=, then destructs
the temporary
The "named return value optimization" (NRVO) can eliminate the temporary in
the second example as well. Specifically, the NRVO optimization eliminates
the need for a temporary by extending the lifetime of the local variable
that foo() returns to be just as long as the lifetime of the temporary that
foo() would have ordinarily returned. In other words, the local variable
that foo() returns - and the temporary that holds foo()'s result (and whose
contents are copied into "s") - are one and the same.
A possible solution would simply to write the function that way so
that another string could be reused:
void foo(std::string& s)
{
s = ""; // to make it empty and forget about the old contents
/* some work on s */
}
This interface for foo() is inconvenient (and inefficient) whenever the
value to be transformed is either const or otherwise needs to be preserved.
A more convenient interface for foo() would separate input from output - and
would accept input by const reference as produce output by return value. And
thanks to the NRVO, the more convenient interface could well be the more
efficient interface as well.
|
OTOH it is very efficient when we want to reuse the string storage.
std::getline() is a good example of that
Diego
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Mathias Gaunard Guest
|
Posted: Thu Apr 19, 2007 2:07 pm Post subject: Re: 'anystate' qualifier |
|
|
On 19 avr, 02:16, "jose.di...@gmail.com" <jose.di...@gmail.com> wrote:
| Quote: |
OTOH it is very efficient when we want to reuse the string storage.
std::getline() is a good example of that
|
Very good point.
A function returning a string in a loop would free and allocate new
storage everytime.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
Mathias Gaunard Guest
|
Posted: Thu Apr 19, 2007 2:08 pm Post subject: Re: 'anystate' qualifier |
|
|
On 17 avr, 22:43, AlbertoBarb...@libero.it (Alberto Ganesh Barbati)
wrote:
| Quote: | 4) the presence of the anystate variable is not exposed in the interface
(it's in the function definition but not in the declaration) so it might
not be available to the caller.
Point 4 is the most interesting. While it might be considered an
advantage (in the end, it should just be an implementation detail of
function foo), this makes it difficult if not impossible to implement
the feature! Yes, because the caller *must* pass in some way to foo()
some information about the need (or lack of) to construct the variable.
|
Indeed, so the function should be qualified specifically also.
Anyway, that's just a crazy idea, it lacks form.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|