Bug with Named Return Values in C++

Johny Cage cage@ok.ru
Sun Jul 21 20:45:00 GMT 2002


Hello bug-gcc,

  I write code that uses named return values
  (http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_5.html)

Code:

#include "stdio.h"

class T
{
  const char *s;
  int len;
public:
  inline T(const char *str,int l):s(str),len(l) {}
  inline const char *GetStr() {return s;}
};

T f();

int main()
{
  printf("%s\n",f().GetStr());
}

T f() return s("zxc",3);
{
  return s;
}

Compile it with

g++ -O6 -Wall -o bug bug.cpp

It should produce output "zxc", but it doesn't.

When I compile it with

g++ -O6 -Wall -S bug.cpp

I see, the function f returns object in stack, but main() waits it in
eax:edx pair. If function f rewrite as follows

T f()// return s("zxc",3);
{
  T s("zxc",3);
  return s;
}

All became Ok.

[~]$ gcc -v
Using builtin specs.
gcc version 2.95.2 19991024 (release)

And

G:\>gcc -v
Reading specs from /cygdrive/c/h/cygwin/bin/../lib/gcc-lib/i686-pc-cygwin/2.95.3-5/specs
gcc version 2.95.3-5 (cygwin special)

-- 
Best regards,
 Johny                          mailto:cage@ok.ru



More information about the Gcc-bugs mailing list