The Artima Developer Community
Sponsored Link

Weblogs Forum
x = f();

2 replies on 1 page. Most recent reply: Aug 5, 2005 10:25 PM by Christopher Diggins

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

x = f(); (View in Weblogs)
Posted: Aug 5, 2005 4:48 PM
Reply to this message Reply
Summary
This is a very simple statement, but I don't like what C++ does with it.
Advertisement
In C++ the following code creates an extra temporary:
class MyClass {
  ...
  int m[1000000]
}

MyClass f() {
  MyClass ret;
  ...
  return ret;
}

int main() {
  MyClass x = f();
}
In Heron I am seriously considering rewriting code like that to behave like:
class MyClass {
  ...
  int m[1000000]
}

void f(MyClass& ret) {
  ...
}

int main() {
  MyClass x;
  f(x);
}
This avoids the creation of a superflous temporary. Could programmers live with the difference?


Tanton Gibbs

Posts: 20
Nickname: tanton
Registered: Aug, 2005

Re: x = f(); Posted: Aug 5, 2005 6:30 PM
Reply to this message Reply
It is my understanding that C++ already works like that. The Named Return Value Optimization should cause C++ to work exactly like your Heron code.

http://www.devx.com/tips/Tip/5716

Christopher Diggins

Posts: 1215
Nickname: cdiggins
Registered: Feb, 2004

Re: x = f(); Posted: Aug 5, 2005 10:25 PM
Reply to this message Reply
> It is my understanding that C++ already works like that.
> The Named Return Value Optimization should cause C++ to
> o work exactly like your Heron code.
>
> http://www.devx.com/tips/Tip/5716

As Mr. Burns of the Simpsons likes to say "Excellent".

Thanks for pointing that out, this makes my life much easier.

Flat View: This topic has 2 replies on 1 page
Topic: Readable Method Calls in Java Previous Topic   Next Topic Topic: The Concept of Concepts

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use