Namespaces in Templated classes

D

daniel_rich

So I have a problem with some existing code, and I am trying to come
up with the cleanest way to handle it.

There is a templated class that was using specialization. Depending on
the type that the class is given the internals of the class were
identical for all versions that were specialized except they used a
different namespace.
template<class RegisterMapBase> class fooClass
{
public:
void foo();
}

void fooClass<RegisterMapA>::foo()
{
namespaceA::doFoo();
}

void fooClass<RegisterMapB>::foo()
{
namespaceB::doFoo();
}

We are handling multiple devices each of which has it's own namespace
for it's register map. Is there a clean way to handle this?
 
D

daniel_rich

So I have a problem with some existing code, and I am trying to come
up with the cleanest way to handle it.

There is a templated class that was using specialization. Depending on
the type that the class is given the internals of the class were
identical for all versions that were specialized except they used a
different namespace.
template<class RegisterMapBase> class fooClass
{
   public:
   void foo();

}

void fooClass<RegisterMapA>::foo()
{
   namespaceA::doFoo();

}

void fooClass<RegisterMapB>::foo()
{
   namespaceB::doFoo();

}

We are handling multiple devices each of which has it's own namespace
for it's register map. Is there a clean way to handle this?

I suppose I should have been more clear on what the problem was. I
think this is ugly, the code inside of those methods is 30-40 lines
long of intricate code with the only differences being the namespaces
used. I was wondering about good ways to refactor it.

Thanks for any ideas
 
D

daniel_rich

Off the top of my head, a using declaration and an explicit
instantiation should do the trick. One instantiation per source file,
unfortunately...

// fooClassA.cpp
#include "fooClass.h"
using namespace A;
template class fooClass<RegisterMapA>;

// fooClassB.cpp
#include "fooClass.h"
using namespace B;
template class fooClass<RegisterMapB>;

This is a C++0x feature, but most compilers have something like it. The
explicit instantiation definition says "instantiate this template
here!". Each instantiation will use the namespace that you need for the
free functions.

Everywhere else that you use the instantiation you need an explicit
instantiation declaration. You'd typically put that in a header:

extern class fooClass<RegisterMapA>;
extern class fooClass<RegisterMapB>;

--
  Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Thanks for the input and information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top