Search code examples
c++design-patternscoding-styleapi-designgeneric-programming

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!


Solution

  • Instead of using class templates for traits in C++11 you can use function declarations (no definition is necessary). Functions can be found using argument-dependent name lookup, so that you can specialise traits for your class in the same namespace where your class is declared.

    This completely removes the nuisance of having to close the namespace of your class, open the traits namespace, specialise the trait for your class using its fully qualified name, close the traits namespace, re-open the namespace of your class. And also removes the need to include the declaration of the primary template.

    Example:

    #include <type_traits>
    
    template<class T> struct Type {};
    
    template<class T>
    void trait_of(Type<T>); // Generic trait version.
    
    namespace N {
    struct A;
    int trait_of(Type<A>); // Trait specialisation for A.
    } // N
    
    int main() {
        using trait_of_a = decltype(trait_of(Type<N::A>{})); // trait_of is found using ADL.
        static_assert(std::is_same<int, trait_of_a>::value, "");
    }
    

    The return type of the trait function can be a container of more types, e.g.:

    template<class T>
    void more_traits(Type<T>); // Generic trait version. Must be specialized.
    
    namespace N {
    struct MoreTraitsOfA {
        using type_X = ...;
        using type_Y = ...;
    };
    MoreTraitsOfA more_traits(Type<A>); // Trait specialisation for A.
    } // N
    
    using MoreTraits = decltype(more_traits(Type<N::A>{})); 
    using type_X = MoreTraits::type_X;
    using type_Y = MoreTraits::type_Y;