[Trilinos-Users] Teuchos:: RCP function arguments, reference, derived types

Phipps, Eric T etphipp at sandia.gov
Wed Jan 20 11:15:12 MST 2010


Hi Nico,

This is correct standard C++ behavior.  The compiler will not convert an object to a non-const reference of another type.  It will only do so for a const-reference or by value, i.e., either of these should be fine:

void myFun(const Teuchos::RCP<BaseType> & x )

or

void myFun( Teuchos::RCP<BaseType>  x )

The former is preferred because it avoids a copy and is probably what you intended anyway as I assume you do not want to change the RCP itself.

-Eric


On 1/20/10 7:15 AM, "Nico Schlömer" <nico.schloemer at ua.ac.be> wrote:

Hi,

I've encountered a strange behavior of Teuchos::RCP and I'm not sure if
it's a bug, a feature, or just stanard C++ behavior. Consider the
following setup:

============= *snip* =============
[...]
void myFun( Teuchos::RCP<BaseType> & x ){
}
[...]

Teuchos::RCP<DerivedType> xx = Teuchos::rcp( new [...] )

f(xx); // Error!
============= *snap* =============

Compiling this, g++ will complain about

============= *snip* =============
error: invalid initialization of reference of type
'Teuchos::RCP<BaseType>&' from expression of type
'Teuchos::RCP<DerivedType>'
============= *snap* =============

It *does work, though, when not defining the function argument as
reference, that, is wiping the "&" above; it works as well when not
passing RCP, but the objects themselves by reference.

A minimal example with Tpetra::{Multi}Vector's is attached.

What's the explanation for this?

Cheers,
Nico

-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://software.sandia.gov/pipermail/trilinos-users/attachments/20100120/26ce4503/attachment.html 


More information about the Trilinos-Users mailing list