CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Programming & Development (
-   -   OF templates < > (

aylalisa December 16, 2014 14:21

OF templates < >
Dear Foamers,

I try to decipher following code snippet, representative for advanced usage of templating in OF (e.g. fvcGrad.H):


template<class Type>   
    64    tmp
    65    <
    66        GeometricField
    67        <typename outerProduct<vector, Type>::type, fvPatchField, volMesh>
    68    > grad
    69    (
    70        const tmp<GeometricField<Type, fvsPatchField, surfaceMesh> >&
    71    );

Is that a template for a class or a function? I miss the class keyword before classname and see a return type of grad(), therefore I think it's a function template whose return type is wrapped tmp<> due to its size and is of class type GeometricField which again is a templated type, but a class which can be of type:
- outerProduct<vector, Type>::type
- fvPatchField
- volMesh
Whereas I have no glue if that is correct or completely wrong.
I can't understand outerProduct<vector, Type>::type, especially the double colon irritates me.

Could somebody help?


deepsterblue December 16, 2014 15:37

Yes, it's a templated function which computes the gradient of a geometric field. The template parameter Type typically refers to a scalar or vector. Now, when you take the gradient of a scalar field, you get a vector field. And when you take the gradient of a vector field, you get a tensor field.
Notice a pattern? This effectively increased the rank of the field by one. That's what the outerProduct operator does - the outer product of a vector (due to the gradient operator) and Type will yield a new type whose rank is increased by one. Since we need to refer to it somehow, we get it using the outerProduct<vector, Type>::type member. When you write it out (the compiler does this automagically for you), it's defined this way:

outerProduct<vector, scalar>::type = vector;


outerProduct<vector, vector>::type = tensor;

At the end of it all, the grad function returns a reference-counted geometric field using the tmp<> class, with a higher rank.

All times are GMT -4. The time now is 01:32.