c++parametersconstantsparameter-passinginput-parameters

passing non-const object for const input parameter?


Currently I am analysing the following code snippet:

fvc::surfaceSum(mag(phi))().internalField() 

with

template<class Type>
tmp<GeometricField<Type, fvPatchField, volMesh> > surfaceSum
(
    const tmp<GeometricField<Type, fvsPatchField, surfaceMesh> >& tssf
)
{
    tmp<GeometricField<Type, fvPatchField, volMesh> > tvf = surfaceSum(tssf());
    tssf.clear();  //If object pointer points to valid object:delete object and 
                           //set pointer to NULL
    return tvf;
}

and

template<class Type, template<class> class PatchField, class GeoMesh>
tmp<GeometricField<scalar, PatchField, GeoMesh> > mag
(
    const GeometricField<Type, PatchField, GeoMesh>& gf
)
{
    tmp<GeometricField<scalar, PatchField, GeoMesh> > tMag
    (
          new GeometricField<scalar, PatchField, GeoMesh>
          (
                IOobject
                (
                      "mag(" + gf.name() + ')',
                      gf.instance(),
                      gf.db(),
                      IOobject::NO_READ,
                      IOobject::NO_WRITE
                ),
                gf.mesh(),
                gf.dimensions()
          )
    );

    mag(tMag(), gf);

    return tMag;
}

and

template<class T>
inline const T& Foam::tmp<T>::operator()() const       
{
    if (isTmp_) // bool isTmp_//Flag for whether object is a temporary or a constant object
    {
          if (!ptr_)   //mutable T* ptr_;  //- Pointer to temporary object   
          {
                FatalErrorIn("const T& Foam::tmp<T>::operator()() const")
                << "temporary deallocated"
                << abort(FatalError);
          }

          return *ptr_;  
    }
    else
    {
          return ref_; //const T& ref_  //- Const reference to constant object
    }
}

and

template<class Type, template<class> class PatchField, class GeoMesh>
typename
Foam::GeometricField<Type, PatchField, GeoMesh>::InternalField 
{                                                                                                                         
    this->setUpToDate();  //Set up to date                                                   
    storeOldTimes();    //Store the old-time fields.
    return *this;
}

Method surfaceSum(...) in the second code snippet expects const tmp<GeometricField<Type, fvsPatchField, surfaceMesh> >& as input parameter, but when going through the other methods the result I get is a non-const parameter tmp<GeometricField<scalar, PatchField, GeoMesh> > tMag(see third codesnippet). Therefore, ist it possible to pass a non-const object for a const input parameter or am I misinterpreting something here?

greetings streight


Solution

  • I can't find any calls to surfaceSum (a few typedefs would greatly improve readability), but, yes, you can pass a non-const object as a const input parameter. The const on the input only says "I won't modify the object that you pass".