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 -- The deal.II project is located at http://www.dealii.org/ For mailing list/forum options, see https://groups.google.com/d/forum/dealii?hl=en --- You received this message because you are subscribed to the Google Groups "deal.II User Group" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
