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:
- 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. - Because
moduleA::internal::AImpl
depends on the traits class of itselftraits<AImpl>
, and my traits templates resides in::project::internal
, so I have to either (1) define a traits template inmoduleA::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.
- Similar to 2,
module::internal::AImpl
depends ontraits<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
add a comment |
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:
- 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. - Because
moduleA::internal::AImpl
depends on the traits class of itselftraits<AImpl>
, and my traits templates resides in::project::internal
, so I have to either (1) define a traits template inmoduleA::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.
- Similar to 2,
module::internal::AImpl
depends ontraits<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
add a comment |
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:
- 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. - Because
moduleA::internal::AImpl
depends on the traits class of itselftraits<AImpl>
, and my traits templates resides in::project::internal
, so I have to either (1) define a traits template inmoduleA::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.
- Similar to 2,
module::internal::AImpl
depends ontraits<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
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:
- 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. - Because
moduleA::internal::AImpl
depends on the traits class of itselftraits<AImpl>
, and my traits templates resides in::project::internal
, so I have to either (1) define a traits template inmoduleA::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.
- Similar to 2,
module::internal::AImpl
depends ontraits<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
c++ design-patterns coding-style api-design generic-programming
asked Jan 18 at 16:08
Jerry MaJerry Ma
13710
13710
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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;
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 havestd::string key_type(A);
andint value_type(A)
and usedecltype(value_type(declval(A)))
to get the type to use. Could you elaborate why I need theType
template? And why I am not seeing this pattern widely in-use elsewhere?
– Jerry Ma
Jan 18 at 18:15
@JerryMa If you usestd::declval<A>()
then the trait function must accept it asA const&
and hence that function would also accept classes derived fromA
, which may or may not be desirable.Type<A>
doesn't match derived classes ofA
.Type
is essentiallyboost::type
or Alexandrescu'sType2Type
, 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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
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 havestd::string key_type(A);
andint value_type(A)
and usedecltype(value_type(declval(A)))
to get the type to use. Could you elaborate why I need theType
template? And why I am not seeing this pattern widely in-use elsewhere?
– Jerry Ma
Jan 18 at 18:15
@JerryMa If you usestd::declval<A>()
then the trait function must accept it asA const&
and hence that function would also accept classes derived fromA
, which may or may not be desirable.Type<A>
doesn't match derived classes ofA
.Type
is essentiallyboost::type
or Alexandrescu'sType2Type
, 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
add a comment |
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;
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 havestd::string key_type(A);
andint value_type(A)
and usedecltype(value_type(declval(A)))
to get the type to use. Could you elaborate why I need theType
template? And why I am not seeing this pattern widely in-use elsewhere?
– Jerry Ma
Jan 18 at 18:15
@JerryMa If you usestd::declval<A>()
then the trait function must accept it asA const&
and hence that function would also accept classes derived fromA
, which may or may not be desirable.Type<A>
doesn't match derived classes ofA
.Type
is essentiallyboost::type
or Alexandrescu'sType2Type
, 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
add a comment |
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;
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;
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 havestd::string key_type(A);
andint value_type(A)
and usedecltype(value_type(declval(A)))
to get the type to use. Could you elaborate why I need theType
template? And why I am not seeing this pattern widely in-use elsewhere?
– Jerry Ma
Jan 18 at 18:15
@JerryMa If you usestd::declval<A>()
then the trait function must accept it asA const&
and hence that function would also accept classes derived fromA
, which may or may not be desirable.Type<A>
doesn't match derived classes ofA
.Type
is essentiallyboost::type
or Alexandrescu'sType2Type
, 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
add a comment |
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 havestd::string key_type(A);
andint value_type(A)
and usedecltype(value_type(declval(A)))
to get the type to use. Could you elaborate why I need theType
template? And why I am not seeing this pattern widely in-use elsewhere?
– Jerry Ma
Jan 18 at 18:15
@JerryMa If you usestd::declval<A>()
then the trait function must accept it asA const&
and hence that function would also accept classes derived fromA
, which may or may not be desirable.Type<A>
doesn't match derived classes ofA
.Type
is essentiallyboost::type
or Alexandrescu'sType2Type
, 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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