|
Boost : |
From: Benoit (benoit.casoetto_at_[hidden])
Date: 2008-08-23 08:25:16
> This sounds exactly like
> what I am trying to do, I already split the declarations into .h files
> and the definitions into .hpp. Can you give a brief example of using a
> single .cpp file for compilation? Is this done like MSVC stdafx files?
Although i use visual studio, I don't know how stdafx files work. I
get rid of them (when autogenerated) as soon as possible !
Now considering your question on how to compile template class
instantiations, the first idea is to use the auto-compilation command
(for a lack of a better expression).
Suppose you have two files myclass.h and myclass.hpp that define a
class template myclass requiring one argument. What you want is to use
myclass<int> somewhere (or everywhere, it doesn't matter) in your
project.
You may create myclass.cpp as follows :
#include "myclass.hpp" // Obviously, myclass.hpp includes myclass.h
template myclass<int>;
In most cases, this is enough. This command instantiates all
non-template member functions of myclass for the particular
instantiation myclass<int>. Note that, if you do have template methods
that are called by non-template methods, you should be fine.
But of course, this is only the simple case. Concepts are pretty handy
when it comes to dealing with *those* not-so-simple cases.
Let's define a concept for what myclass should do (in
concept_myclass.hpp for example)
template<typename verify_class>
class concept_myclass
{
void constraints( verify_class& verify )
{
compute_type i = verify.compute( 3, 2 );
double d = verify.get_square_root( i );
// ... and so forth
}
typedef typename verify_class::compute_type compute_type;
};
This is a fairly simple example, but you get the idea. Now you can
create a cpp file to "compile" your hpp file :
#include "myclass.hpp"
#include "concept_myclass.hpp"
template concept_myclass<myclass<int> >;
Now everything should work the way you wish it would (i hope !). At
some point i'll try and make this really compatible with the concept
check library, but i haven't had time to look into that so far.
I personnally use a configuration file which contains typedefs for
pretty much all template class instantiations. I believe It reduces
the number of possible errors when you decide to change the template
arguments. Using this, the previous cpp file becomes :
#include "myclass.hpp"
#include "concept_myclass.hpp"
#include "config.hpp"
template concept_myclass<config::myclass>;
That's all !!!
Benoît Casoetto
David : i take good note of boost recommandations on file naming, thanks !
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk