CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM (http://www.cfd-online.com/Forums/openfoam/)
-   -   Convert templated class into vector (http://www.cfd-online.com/Forums/openfoam/103225-convert-templated-class-into-vector.html)

Peter Müller June 14, 2012 04:50

Convert templated class into vector
 
Hello together

First sorry for creating a new thread but I could not find what I was looking for.

I'm creating a new boundary and the boundary should be build up as templated version so to make it usable for any kind of quantity. The problem now is that I would like to do a coordinate transform to the velocity.
Therefore I need to grab the internal field like

Field<Type> iField = this->internalField();

In a next step I wanted to create a vectorField if "Type" is vector

if(pTraits<Type>::rank ==1)
{
Field<vector> myField = iField;
}

As you may probably already notice this want work since iField is templated so it can't be given to a vector field since the compiler doesn't know that my if-statement only allows the operation if Type==vector.

Has anybody an idea how to circumvent this problem?

Many thanks
Peter

olesen June 15, 2012 02:45

Quote:

Originally Posted by Peter Müller (Post 366414)
Hello together

First sorry for creating a new thread but I could not find what I was looking for.

I'm creating a new boundary and the boundary should be build up as templated version so to make it usable for any kind of quantity. The problem now is that I would like to do a coordinate transform to the velocity.
Therefore I need to grab the internal field like

Field<Type> iField = this->internalField();

In a next step I wanted to create a vectorField if "Type" is vector

if(pTraits<Type>::rank ==1)
{
Field<vector> myField = iField;
}

As you may probably already notice this want work since iField is templated so it can't be given to a vector field since the compiler doesn't know that my if-statement only allows the operation if Type==vector.

Has anybody an idea how to circumvent this problem?

Many thanks
Peter



You are correct. Since things are templated, you would normally not like that. What you need is a template specialization for the type vector that implements what you want. If you haven't worked with this before, it may take you a few attempts to get it going. The important points:
  • The template specialization itself must be defined within a namespace.
  • The template specialization must be seen before the generic template form.
A trivial example can be see in the OPENFOAM UList<T> implementation. Take a look at the [] operator (the const implementation).
From the UList.H:
Code:


inline const T& operator[](const label) const;

Within UListI.H, you will find the specialization and the generic form:

Code:


namespace Foam
{
    // Template specialization for bool
    template<>
    inline const bool& Foam::UList<bool>::operator[](const label i) const
    {
    ...
    }
} // end of namespace Foam
 
// const element access
template<class T>
inline const T& Foam::UList<T>::operator[](const label i) const
{
...
}


In your particular case, you can either specialize the entire method for various vector, or factor it out as a private method. In either case, you can view the specialization as being a compile-time version of an 'if' or a 'switch'.

I hope this helps you get going.


All times are GMT -4. The time now is 05:56.