2
void foo(const C &);
foo(C());

In this case, temporary C object lives until the end of foo().

But my question is:

struct C { operator int(); }
void bar(int i);
bar(C());

In that case, Does temporary C object live until the end of bar()? If not, Is there any way to make temporary object live until the end of bar()?

edit: Thanks to user2109558, I know the code doesn't work. Then does the following code work well?

void bar(int i);
void bar(C &&c) { bar(c); }
ikh
  • 10,119
  • 1
  • 31
  • 70

1 Answers1

-2

No!the codebar(C()); is equal to int num = C(); bar(num); So , temporary C object does not live until the end of bar() you have to change bar function parameter.such as void bar(const C &) or add global C object variable eg:

C c;
void bar(int i) {
 ..... 

}

but no suggestion do it.

xyCoder
  • 110
  • 10