Reputation: 93476
I originally had a number of signal processing filter classes that were identical apart from a few constants defining the filter characteristic, so I decided to change these to a template class for maintainability and extensibility. There are performance and memory management reasons for preferring a template over constructor arguments in this case; it is for an embedded system.
Consequently I have a template class of the form:
template <int SIZE, int SCALE_MULTIPLIER, int SCALE_SHIFT>
class cBoxcarFilter
{
public:
// Allow access to SIZE at runtime.
static const int FILTER_SIZE = SIZE ;
...
}
Which I explicitly instantiate thus, for example:
template class cBoxcarFilter<8, 1, 3>
The problem is when I need to access the FILTER_SIZE member it requires:
cBoxcarFilter<8, 1, 3>::FILTER_SIZE
which rather makes access to FILTER_SIZE redundant since it must be repeated in the arguments. My solution to this problem is this:
// Create an alias for filter
#define cSpecialistBoxcarFilter cBoxcarFilter<8, 1, 3>
template class cSpecialistBoxcarFilter ;
then I can access FILTER_SIZE thus:
cSpecialistBoxcarFilter::FILTER_SIZE
this also has the advantage of meaningful unique names for each filter instance as in the original non-templated versions, but it seems somewhat smelly to me using a macro that looks like a class since it has different scope semantics.
Is there a better way of creating alias class names for a template instance?
Upvotes: 1
Views: 586
Reputation: 42872
You can ask concrete instances (objects) of your class for their filter size:
template <int X> class A
{
public:
int y;
static const int size = X;
};
int main(int, char**)
{
A<3> a;
printf("size is %i\n", a.size);
}
result
size is 3
Upvotes: 0
Reputation: 272507
Yes, typedef
!
typedef cBoxcarFilter<8, 1, 3> cSpecialistBoxcarFilter;
Upvotes: 10