traits class, namespace and forward declaration












1















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!










share|improve this question



























    1















    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!










    share|improve this question

























      1












      1








      1








      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!










      share|improve this question














      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!







      c++ design-patterns coding-style api-design generic-programming






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 18 at 16:08









      Jerry MaJerry Ma

      13710




      13710
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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;





          share|improve this answer


























          • Thank you. Didn't know I can do this. It seems very convenient if a traits class contains only a few member types. To my situation, I can have std::string key_type(A); and int value_type(A) and use decltype(value_type(declval(A))) to get the type to use. Could you elaborate why I need the Type template? And why I am not seeing this pattern widely in-use elsewhere?

            – Jerry Ma
            Jan 18 at 18:15













          • @JerryMa If you use std::declval<A>() then the trait function must accept it as A const& and hence that function would also accept classes derived from A, which may or may not be desirable. Type<A> doesn't match derived classes of A. Type is essentially boost::type or Alexandrescu's Type2Type, its main purpose is to discriminate function overloads, just like what we do here. It allows to pass a type as a function argument without having to construct a value of that type somehow, disables (unexpected) conversions, and most conveniently, it doesn't preclude ADL.

            – Maxim Egorushkin
            Jan 18 at 21:53













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54257598%2ftraits-class-namespace-and-forward-declaration%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          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;





          share|improve this answer


























          • Thank you. Didn't know I can do this. It seems very convenient if a traits class contains only a few member types. To my situation, I can have std::string key_type(A); and int value_type(A) and use decltype(value_type(declval(A))) to get the type to use. Could you elaborate why I need the Type template? And why I am not seeing this pattern widely in-use elsewhere?

            – Jerry Ma
            Jan 18 at 18:15













          • @JerryMa If you use std::declval<A>() then the trait function must accept it as A const& and hence that function would also accept classes derived from A, which may or may not be desirable. Type<A> doesn't match derived classes of A. Type is essentially boost::type or Alexandrescu's Type2Type, its main purpose is to discriminate function overloads, just like what we do here. It allows to pass a type as a function argument without having to construct a value of that type somehow, disables (unexpected) conversions, and most conveniently, it doesn't preclude ADL.

            – Maxim Egorushkin
            Jan 18 at 21:53


















          1














          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;





          share|improve this answer


























          • Thank you. Didn't know I can do this. It seems very convenient if a traits class contains only a few member types. To my situation, I can have std::string key_type(A); and int value_type(A) and use decltype(value_type(declval(A))) to get the type to use. Could you elaborate why I need the Type template? And why I am not seeing this pattern widely in-use elsewhere?

            – Jerry Ma
            Jan 18 at 18:15













          • @JerryMa If you use std::declval<A>() then the trait function must accept it as A const& and hence that function would also accept classes derived from A, which may or may not be desirable. Type<A> doesn't match derived classes of A. Type is essentially boost::type or Alexandrescu's Type2Type, its main purpose is to discriminate function overloads, just like what we do here. It allows to pass a type as a function argument without having to construct a value of that type somehow, disables (unexpected) conversions, and most conveniently, it doesn't preclude ADL.

            – Maxim Egorushkin
            Jan 18 at 21:53
















          1












          1








          1







          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;





          share|improve this answer















          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;






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 18 at 22:19

























          answered Jan 18 at 16:51









          Maxim EgorushkinMaxim Egorushkin

          86.4k11100183




          86.4k11100183













          • Thank you. Didn't know I can do this. It seems very convenient if a traits class contains only a few member types. To my situation, I can have std::string key_type(A); and int value_type(A) and use decltype(value_type(declval(A))) to get the type to use. Could you elaborate why I need the Type template? And why I am not seeing this pattern widely in-use elsewhere?

            – Jerry Ma
            Jan 18 at 18:15













          • @JerryMa If you use std::declval<A>() then the trait function must accept it as A const& and hence that function would also accept classes derived from A, which may or may not be desirable. Type<A> doesn't match derived classes of A. Type is essentially boost::type or Alexandrescu's Type2Type, its main purpose is to discriminate function overloads, just like what we do here. It allows to pass a type as a function argument without having to construct a value of that type somehow, disables (unexpected) conversions, and most conveniently, it doesn't preclude ADL.

            – Maxim Egorushkin
            Jan 18 at 21:53





















          • Thank you. Didn't know I can do this. It seems very convenient if a traits class contains only a few member types. To my situation, I can have std::string key_type(A); and int value_type(A) and use decltype(value_type(declval(A))) to get the type to use. Could you elaborate why I need the Type template? And why I am not seeing this pattern widely in-use elsewhere?

            – Jerry Ma
            Jan 18 at 18:15













          • @JerryMa If you use std::declval<A>() then the trait function must accept it as A const& and hence that function would also accept classes derived from A, which may or may not be desirable. Type<A> doesn't match derived classes of A. Type is essentially boost::type or Alexandrescu's Type2Type, its main purpose is to discriminate function overloads, just like what we do here. It allows to pass a type as a function argument without having to construct a value of that type somehow, disables (unexpected) conversions, and most conveniently, it doesn't preclude ADL.

            – Maxim Egorushkin
            Jan 18 at 21:53



















          Thank you. Didn't know I can do this. It seems very convenient if a traits class contains only a few member types. To my situation, I can have std::string key_type(A); and int value_type(A) and use decltype(value_type(declval(A))) to get the type to use. Could you elaborate why I need the Type template? And why I am not seeing this pattern widely in-use elsewhere?

          – Jerry Ma
          Jan 18 at 18:15







          Thank you. Didn't know I can do this. It seems very convenient if a traits class contains only a few member types. To my situation, I can have std::string key_type(A); and int value_type(A) and use decltype(value_type(declval(A))) to get the type to use. Could you elaborate why I need the Type template? And why I am not seeing this pattern widely in-use elsewhere?

          – Jerry Ma
          Jan 18 at 18:15















          @JerryMa If you use std::declval<A>() then the trait function must accept it as A const& and hence that function would also accept classes derived from A, which may or may not be desirable. Type<A> doesn't match derived classes of A. Type is essentially boost::type or Alexandrescu's Type2Type, its main purpose is to discriminate function overloads, just like what we do here. It allows to pass a type as a function argument without having to construct a value of that type somehow, disables (unexpected) conversions, and most conveniently, it doesn't preclude ADL.

          – Maxim Egorushkin
          Jan 18 at 21:53







          @JerryMa If you use std::declval<A>() then the trait function must accept it as A const& and hence that function would also accept classes derived from A, which may or may not be desirable. Type<A> doesn't match derived classes of A. Type is essentially boost::type or Alexandrescu's Type2Type, its main purpose is to discriminate function overloads, just like what we do here. It allows to pass a type as a function argument without having to construct a value of that type somehow, disables (unexpected) conversions, and most conveniently, it doesn't preclude ADL.

          – Maxim Egorushkin
          Jan 18 at 21:53




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54257598%2ftraits-class-namespace-and-forward-declaration%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage