Ask for deal.II method to write a template class in separate files

21 views
Skip to first unread message

Qing Yin

unread,
Dec 22, 2017, 1:44:29 PM12/22/17
to deal.II User Group
Dear deal.ii community,

I am asking for the method to declare and define a dimension-independent template class in separate files . Because from all the book/tutorial on templates I have read,  it recommends that template declaration and definition must all be in the same code file. However, I surprisely found deal.ii separates the template classes into a header file and a source file without any problem. Could you tell me that nice approach?

Thank you for your time and consideration!

Sincerely,

Qing


Bruno Turcksin

unread,
Dec 22, 2017, 2:21:51 PM12/22/17
to deal.II User Group
Qing


On Friday, December 22, 2017 at 1:44:29 PM UTC-5, Qing Yin wrote:
I am asking for the method to declare and define a dimension-independent template class in separate files . Because from all the book/tutorial on templates I have read,  it recommends that template declaration and definition must all be in the same code file. However, I surprisely found deal.ii separates the template classes into a header file and a source file without any problem. Could you tell me that nice approach?
It's because we do explicit template instantiation of many template parameters. Let's say that you have a Triangulation<dim> we know that we will only ever need dim = 1, 2, and 3. You can write the implementation in the source file and at the bottom of the source file, you will write something like

template  class Triangulation<1>;
template  class Triangulation<2>;
template  class Triangulation<3>;

Notice that Triangulation<4> does not exist and it is impossible for a user to instantiate it. Sometimes this is a problem. Let's take the example of Vector. We want to instantiate Vector for double and float but we also want to let the user work with long double. In this case, we  have vector.h, vector.templates.h (with the implementation), and vector.cc (with the explicit instantiation). If you want to use Vector<double> you can simply include <vector.h> but if you want to use Vector<long double> you need to include <vector.h> and <vector.templates.h> (so that the declaration and the definition are together).

Best,

Bruno

Qing Yin

unread,
Dec 22, 2017, 3:34:35 PM12/22/17
to deal.II User Group
Dear Bruno,

Thank you! Clear explanation and very helpful.

Best,

Qing

在 2017年12月22日星期五 UTC-7下午12:21:51,Bruno Turcksin写道:
Reply all
Reply to author
Forward
0 new messages