Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Texture::CopyToImage return by value  (Read 2454 times)

0 Members and 1 Guest are viewing this topic.

deadalnix

  • Newbie
  • *
  • Posts: 45
    • View Profile
sf::Texture::CopyToImage return by value
« on: December 01, 2011, 10:59:58 pm »
It would be nicer if it could return by reference. In some cases, you have to create temporary variables, for exemple, to pass the returned image to a function that require a reference (invalid initialization of non-const reference of type ‘sf::Image&’ from an rvalue of type ‘sf::Image’).

In most cases, it doesn't change anything as the compiler is able to avoid the copy.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
sf::Texture::CopyToImage return by value
« Reply #1 on: December 01, 2011, 11:03:46 pm »
Return a reference to what? The function creates a new sf::Image instance and returns it to the caller. It is not stored anywhere else. Return by value is the only option here.

To use a function that expects a non-const reference, you have to use a named variable, you can't pass a temporary -- it is forbidden.

That's basic C++ rules, you should get more familiar with them before trying to do things that you shouldn't do ;)
Laurent Gomila - SFML developer

deadalnix

  • Newbie
  • *
  • Posts: 45
    • View Profile
sf::Texture::CopyToImage return by value
« Reply #2 on: December 02, 2011, 12:36:11 am »
Well the return area is create by the caller on the stack. This is why you don't have any copy when you do :

Image i = texture.CopyToImage(); // Copy constructor isn't called here

This copy constructor isn't called because the memory are where the image is intanciated is created by the caller and passeed to the function as an hidden parameter (a pointer). So you can return a reference in this particular case.

Nevermind, this isn't a big deal.

EDIT: Well that remembers me why I do less C++ and looks for alternatives in things like D :D Obviously, the area on the stack isn't create if you return by reference. My C++ is a bit rusty, you are right. keeping the original post for the reccord.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
sf::Texture::CopyToImage return by value
« Reply #3 on: December 02, 2011, 04:50:25 pm »
Quote from: "deadalnix"
This copy constructor isn't called because the memory are where the image is intanciated is created by the caller and passeed to the function as an hidden parameter (a pointer).
This isn't exactly true. The compiler is allowed to omit the copy as an optimization, but not required to do so. In particular, a copy constructor must still exist for these semantics to be valid.

Quote from: "deadalnix"
So you can return a reference in this particular case.
No, you cannot. If you return a reference, it still refers to an object that goes out of scope at the end of the function. You may not even return a rvalue reference for the same reason. However, the compiler may use (N)RVO or move semantics to get rid of unnecessary copies -- but you must return the object by value.

Argh, sorry, saw your correction only at the end. But I leave the explanation for other users reading this thread.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: