As others have said, what you want is not possible right now with C ++ vanilla for arbitrary types. But if you can provide specific compilation information along with the types you need to use in your definition, you can do what you are trying to do.
You can do this with boost fusion library adapters that let you adapt existing structures to merge containers or define new structures that model the merge container. Then you can use any boost :: mpl algorithms that you want to perform the types of compile time checks you want to do.
Consider this example using your struct foo and your desired has_type compile-time algorithm:
#include <boost/fusion/adapted/struct/define_struct.hpp> #include <boost/mpl/contains.hpp> BOOST_FUSION_DEFINE_STRUCT( (your_namespace), foo, (int, a) (char, c)) template<typename source_type, typename search_type> struct has_type { typedef typename boost::mpl::contains<source_type, search_type>::type value_type; static const bool value = value_type::value; }; #include <iostream> int main() { bool foo_has_int_pointer = has_type<your_namespace::foo, int*>::value; bool foo_has_int = has_type<your_namespace::foo, int>::value; std::cout << "foo_has_int_pointer: " << foo_has_int_pointer << "\n"; std::cout << "foo_has_int: " << foo_has_int << "\n"; your_namespace::foo my_foo; my_foo.a = 10; my_foo.c = 'x'; std::cout << "my_foo: " << my_foo.a << ", " << my_foo.c; }
View the output or clutter with an example here: http://ideone.com/f0Zc2M
As you can see, you use the BOOST_FUSION_DEFINE_STRUCT macro to determine the struct whose members you want to use at compile time. fusion provides several other macros to define structures like this, as well as macros to adapt already defined structures. Check them out here .
Of course, you probably can already say what's down here. has_type will only work if source_type is the sequence boost :: mpl / boost :: fusion. For you, this means that any type that you want to reason at compile time this way you need to define or adapt with macros. This may not be acceptable to you if you are writing a library designed to work with arbitrary library specific types.
source share