Odd (and deprecated) g++ extensions

I’ve been spending the evening reading about libstdc++ (mostly the documentation) and how to do stuff The Right Way (the documentation is actually interesting, you should at least take a look at it). Now I feel the need to share some stuff that’s a bit more odd then what you see in the documentation.

First: “the named return value extension”. The idea is to avoid creating a temporary variable that’s assigned a value and then returned by value, thus creating an unnecessary copy. A small example:

std::list<int> foo(int j) return l();
{
  l.push_back(j);
}

This example will create just one list and append j to that. Without this extension a local list needs to be created, j appended to that and the copy constructor invoked when returning. This extension is deprecated in g++ 3.3 and removed in g++ 4.0.

My other example is the “min and max operators”. No, this is not about min and max macros nor the std::min and std::max functions. This is the min and max operators.

int min = x <? y;
int max = x >? y;

There is of course also a short-hand version for assigning one of the variables the smallest (or biggest) value of the two.

x <?= y;
x >?= y;

They are all deprecated in g++ 4.0.

Posted Tuesday, March 28th, 2006 under programming.

Tags: ,

One comment so far

  1. One little trick I like for skipping a copy on return is to return a smart pointer (boost::shared_ptr or std::auto_ptr). It is also exception-safe in that it will only return a value if the entire function was completed without an exception. But of course you pay the price in efficiency with a memory allocation.