Tasking C166 C++ compiler & temporary objects in function call parameters

P

perdubug

Somebody told me that Tasking C166 C++ compiler has problems with
temporary objects in function call parameters. He gave me below
examples:

Case 1:Wrong:
rc = foo(&bar());

Case 2:Right:
bar b;
rc=foo(&b);

But he didn't know why...Can anyone show me why? why case 1 is wrong
with Tasking C166 C++ compiler? How about other C++ compilers?

Thanks and Best Regards,
PerduBug
 
A

Andrey Tarasevich

Somebody told me that Tasking C166 C++ compiler has problems with
temporary objects in function call parameters. He gave me below
examples:

Case 1:Wrong:
rc = foo(&bar());

Case 2:Right:
bar b;
rc=foo(&b);

But he didn't know why...Can anyone show me why? why case 1 is wrong
with Tasking C166 C++ compiler? How about other C++ compilers?
...

The problem with the above code has nothing to do with any function call
parameters at all. In the first case you are trying to apply the built-in
operator '&' to a temporary object - '&bar()'. Built-in operator '&' cannot be
applied to temporary objects, because '&' requires an lvalue. Temporary objects
are not lvalues. That's it.

You can rewrite your code as follows

1.
&bar();

2. bar b;
&b;

and get the same problem without any function calls.
 
F

Frederick Gotham

Perdubug posted:
Somebody told me that Tasking C166 C++ compiler has problems with
temporary objects in function call parameters. He gave me below
examples:

Case 1:Wrong:
rc = foo(&bar());


You can't take the address of an R-value.

Case 2:Right:
bar b;
rc=foo(&b);


If you only need a const temporary, then:

class Arb {};

void Func(Arb const *) {}

int main()
{
Func(&static_cast<Arb const&>(Arb()));
}

If you need a non-const temporary, then perhaps try something like the
following:

template<class T>
struct Temp {

T obj;

Temp() : obj() {}

T *operator&() { return &obj; }
};

class Arb {};

void Func(Arb*) {}

int main()
{
Func(&Temp<Arb>());
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,109
Messages
2,570,671
Members
47,263
Latest member
SyreetaGru

Latest Threads

Top