C
Christof Warlich
Hi,
I just found the following code that shows how to determine whether a
class has a certain member:
#include <iostream>
typedef char (&no_tag)[1];
typedef char (&yes_tag)[2];
template <typename T, void (T::*)()> struct ptmf_helper {};
template<typename T> no_tag has_member_foo_helper(...);
template<typename T>
yes_tag has_member_foo_helper(ptmf_helper<T, &T::foo>* p);
template<typename T> struct has_member_foo {
static const bool value = sizeof(has_member_foo_helper<T>(0)) ==
sizeof(yes_tag);
};
struct HasNoFoo {};
struct HasFoo {void foo();};
struct DerivedFromHasFoo: HasFoo {};
int main() {
std::cout << has_member_foo<HasNoFoo>::value << std::endl;
std::cout << has_member_foo<HasFoo>::value << std::endl;
std::cout << has_member_foo<DerivedFromHasFoo>::value << std::endl;
}
But what I'd really need is some "provides_member" functionaliy, i.e.
something that would also evaluate to true for any derived class of
class HasFoo. Anyone having an idea how this could be accomplished?
Furthermore, is anyone aware whether something like has_member and
provides_member is available in Boost? I would really miss it as part of
Boost's type_traits, but couldn't find it.
Finally, can anyone recommend a good overview on the functionality
provided by Boost, most notably with respect to template metaprogramming?
Thanks,
Christof
I just found the following code that shows how to determine whether a
class has a certain member:
#include <iostream>
typedef char (&no_tag)[1];
typedef char (&yes_tag)[2];
template <typename T, void (T::*)()> struct ptmf_helper {};
template<typename T> no_tag has_member_foo_helper(...);
template<typename T>
yes_tag has_member_foo_helper(ptmf_helper<T, &T::foo>* p);
template<typename T> struct has_member_foo {
static const bool value = sizeof(has_member_foo_helper<T>(0)) ==
sizeof(yes_tag);
};
struct HasNoFoo {};
struct HasFoo {void foo();};
struct DerivedFromHasFoo: HasFoo {};
int main() {
std::cout << has_member_foo<HasNoFoo>::value << std::endl;
std::cout << has_member_foo<HasFoo>::value << std::endl;
std::cout << has_member_foo<DerivedFromHasFoo>::value << std::endl;
}
But what I'd really need is some "provides_member" functionaliy, i.e.
something that would also evaluate to true for any derived class of
class HasFoo. Anyone having an idea how this could be accomplished?
Furthermore, is anyone aware whether something like has_member and
provides_member is available in Boost? I would really miss it as part of
Boost's type_traits, but couldn't find it.
Finally, can anyone recommend a good overview on the functionality
provided by Boost, most notably with respect to template metaprogramming?
Thanks,
Christof