Skip to content

Since v2212: reference to typeName is ambiguous (refCast to const fvPatchScalarField)

In the preCICE OpenFOAM adapter, version v1.2.1, in this file, I have the following code:

// Get the boundary field of Temperature on the patch
const fvPatchScalarField& TPatch(
        refCast<const fvPatchScalarField>(
          T_->boundaryField()[patchID]));

This compiles with v2206 (and several previous versions), but fails since v2212, only referencing OpenFOAM-internal code. In both cases, I am using GCC 11.3.0 on Ubuntu 22.04. Error summary:

In file included from /usr/lib/openfoam/openfoam2212/src/OpenFOAM/lnInclude/token.H:49,
                 from /usr/lib/openfoam/openfoam2212/src/OpenFOAM/lnInclude/Istream.H:50,
                 from /usr/lib/openfoam/openfoam2212/src/OpenFOAM/lnInclude/ISstream.H:42,
                 from /usr/lib/openfoam/openfoam2212/src/OpenFOAM/lnInclude/IOstreams.H:41,
                 from ./Utilities.H:40,
                 from ./CouplingDataUser.H:4,
                 from CHT/SinkTemperature.H:4,
                 from CHT/SinkTemperature.C:1:
/usr/lib/openfoam/openfoam2212/src/OpenFOAM/lnInclude/typeInfo.H: In instantiation of ‘Type& Foam::refCast(U&) [with Type = const Foam::fvPatchField<double>; U = const Foam::fvPatchField<double>]’:
CHT/SinkTemperature.C:28:46:   required from here
/usr/lib/openfoam/openfoam2212/src/OpenFOAM/lnInclude/typeInfo.H:151:37: error: reference to ‘const Foam::fvPatchField<double>::typeName’ is ambiguous
  151 |             << " to type " << Type::typeName
      |                                     ^~~~~~~~

with candidates being const char* const Foam::FieldBase::typeName and const Foam::word Foam::fvPatchFieldBase::typeName. This is the related template:

//- A dynamic_cast (for references) that generates FatalError on failed casts,
//- uses the virtual type() method for error messages.
template<class Type, class U>
inline Type& refCast(U& obj)
{
    U* p = &obj;
    Type* casted = dynamic_cast<Type*>(p);

    if (!casted)
    {
        FatalErrorInFunction
            << "Attempt to cast type " << obj.type()
            << " to type " << Type::typeName
            << abort(FatalError);
    }

    return *casted;
}

I do not completely understand how this is triggered at the moment, but it looks to me like a regression (or I was always doing something wrong). I do not see anything related in the Developer Upgrade Guide.

Full log: v2212-error.log