Home » Developing U++ » U++ Developers corner » Should the pick semantics be changed?
Re: Should the pick semantics be changed? [message #42546 is a reply to message #42364] |
Sun, 23 March 2014 22:02 |
Lance
Messages: 621 Registered: March 2007
|
Contributor |
|
|
piotr5 wrote on Sun, 09 March 2014 08:28 | well, to formalize a bit more concretely what I said, I believe return statement should at compile-time distinguish between return values local to the function which are not static, and all the return-values which remain in scope after the end of this function respectively could get into scope again (like static values or private members and such). and then based on this distinction the actual return-value should either be constructed with implicit cast to r-value or with the persistent const-l-value-reference constructor.
|
Not sure if I get you correctly but I think it's logical to put the burden on programmers as is. The two return types are different (and binary incompitable): one is an object and the other is a reference to object. Let's say C is a moveable class. There are some global/static objects of C which we can choose from one only from base on input parameter, then it's logical for the programmer the let the reference be the return type.
/*const*/ C& getC(int type);
Now suppose we need to return some composed(not presently existing) C for some special input types(e.g., when type<100, there are corresponding existing global/static C objects, when type>=100, one has to be composed accordingly), the most economically way is to return a temporary C object if adding a new function to handle the case when type>=100 is not desirable.
Now if you declare the function prototype using the second way, any reasonable compiler would not be able to change it to return reference (the first) even if it's smart enough to detect that inside you implementation for getC actually involves only global/static C objects.
So a programmer has to make this decision, the compiler has to respect the programmer even if doing it otherwise is more efficient.
However, in the case a local object is returned, compiler may take the burden and save the programmer some extra key stroke., eg
C getC(int type)
{
if( (unsigned)type<100 )
return globalCs[type]; // return reference here is
// impossible as it will make
// the subsequent code impossible
C c;
createCBasedOnType(type, c);
return c; // instead of
// return std::move(c);
// as required by current standard to take
// advantage of the supposedly more efficient
// move semantic
}
|
|
|
|
|
Should the pick semantics be changed?
By: piotr5 on Tue, 04 March 2014 11:00
|
|
|
Re: Should the pick semantics be changed?
By: mirek on Tue, 04 March 2014 12:12
|
|
|
Re: Should the pick semantics be changed?
By: piotr5 on Tue, 04 March 2014 23:14
|
|
|
Re: Should the pick semantics be changed?
By: mirek on Wed, 05 March 2014 08:54
|
|
|
Re: Should the pick semantics be changed?
By: piotr5 on Wed, 05 March 2014 10:27
|
|
|
Re: Should the pick semantics be changed?
By: mirek on Wed, 05 March 2014 12:02
|
|
|
Re: Should the pick semantics be changed?
By: mirek on Fri, 07 March 2014 09:00
|
|
|
Re: Should the pick semantics be changed?
By: piotr5 on Fri, 07 March 2014 16:23
|
|
|
Re: Should the pick semantics be changed?
By: mirek on Fri, 07 March 2014 16:44
|
|
|
Re: Should the pick semantics be changed?
By: mirek on Sun, 09 March 2014 09:34
|
|
|
Re: Should the pick semantics be changed?
By: piotr5 on Sun, 09 March 2014 13:28
|
|
|
Re: Should the pick semantics be changed?
By: Lance on Sun, 23 March 2014 22:02
|
|
|
Re: Should the pick semantics be changed?
By: piotr5 on Tue, 25 March 2014 19:35
|
|
|
Re: Should the pick semantics be changed?
By: Lance on Wed, 26 March 2014 02:37
|
|
|
Re: Should the pick semantics be changed?
By: Lance on Wed, 26 March 2014 02:48
|
|
|
Re: Should the pick semantics be changed?
By: piotr5 on Wed, 26 March 2014 10:35
|
|
|
Re: Should the pick semantics be changed?
By: mirek on Wed, 26 March 2014 11:38
|
|
|
Re: Should the pick semantics be changed?
By: Lance on Wed, 26 March 2014 21:25
|
|
|
Re: Should the pick semantics be changed?
By: Lance on Wed, 26 March 2014 21:21
|
Goto Forum:
Current Time: Fri Nov 01 01:35:36 CET 2024
Total time taken to generate the page: 0.02613 seconds
|