vendredi 18 janvier 2019

traits class, namespace and forward declaration

I am currently having trouble using namespaces with traits classes. Here is my tentative code structure:

namespace project {

namespace internal {
template<typename T> struct traits;

} // internal

namespace moduleA {

namespace internal {

class AImpl {

using some_typeA = traits<A>::some_type;
using some_typeAImpl = traits<AImpl>::some_type;
// where to put the traits specialization?? How the forward declaration could be done?

};

} // internal

class A {

A(): imp(new internal::AImpl()) {}
private:
    internal::AImpl* imp;
};

} // moduleA

} // project

Here are my questions and I am looking for suggestions to make this code better follow the established conventions and best practices:

  1. I am defining two internal namespaces, ::project::internal and ::project::moduleA::internal, is this a bad practice? My concern on this is that with two levels it might be easier for user to browse the documentation from doxygen, as all the moduleA related stuff, both moduleA::internal and not, are grouped together.
  2. Because moduleA::internal::AImpl depends on the traits class of itself traits<AImpl>, and my traits templates resides in ::project::internal, so I have to either (1) define a traits template in moduleA::internal and specialize it; (2) define the traits specialization in ::project::internal. For this, I'll need forward-declare AImpl. How exactly should it be done for each of the case (1) or (2)? Does that mean I have to write code like this:
namespace project {
namespace moduleA {class A;}
namespace internal {
template<>
struct traits<module::A> {};
}
namespace moduleA {
... // more code
}
}

It looks like I am making too much use of namespace {} clauses.

  1. Similar to 2, module::internal::AImpl depends on traits<A>, again I need to forward declare A, so the same problem.

I'd greatly appreciate you help on this, thank you!

Aucun commentaire:

Enregistrer un commentaire