Original Post
typedef typename boost::mpl::if_
<
boost::is_class<Type>,
typename Type::value_type,
Type
>::type value_type;
typedef typename boost::mpl::if_
<
boost::is_class<Type>,
typename Type::value_type,
Type
>::type value_type;
struct sfinae{ typedef char one; typedef struct { char c[2] } two;};template<typename T>struct has_value_type{private: template<typename T> struct wrap { }; template<typename T> sfinae::one test(Wrap<typename T::value_type>*); template<typename T> sfinae::two test(...);public: // if T has a value_type then test(0) will call the first // version and return sfinae::one otherwise it will call // the second version enum { value = sizeof(test(0)) == sizeof(sfinae::one) };};template<typename T, bool HasValueType = has_value_type<T>::value>struct get_value_type;template<typename T>struct get_value_type<T, true>{ typedef typename T::value_type value_type;};template<typename T>struct get_value_type<T, false>{ typedef T value_type;}template<typename T>struct stuff{ // meta-function that returns T::value_type template<typename Type> struct use_value_type { typedef typename Type::value_type type; }; // meta_function that returns T template<typename Type> struct use_type { typedef Type type; }; typedef typename boost::mpl::eval_if < // if it is a class boost::is_class<T>, // boost will use return the ::type of this type use_value_type<T>, // otherwise it returns the ::type of this type use_type<T> >::type type;};#ifndef HPP_CONTAINER_TRAITS_03152007#define HPP_CONTAINER_TRAITS_03152007namespace tmp{namespace detail{struct sfinae_types{ typedef char one; typedef struct { char c[2]; } two;};template <typename T> struct Wrap {};} // end namespace detail#define define_type_check(type_name) template<class T> struct has_##type_name { private: template<class T> static detail::sfinae_types::one test(detail::Wrap<typename T::type_name>*); template<class T> static detail::sfinae_types::two test(...); template<typename T, bool HasTypeName> struct get_type_name; template<typename T> struct get_type_name<T, true> { typedef typename T::type_name type; }; template<typename T> struct get_type_name<T, false> { typedef T type; }; public: static const bool value = sizeof(test<T>(0)) == sizeof(detail::sfinae_types::one); typedef typename get_type_name<T, value>::type type; };define_type_check(allocator_type);define_type_check(const_iterator);define_type_check(iterator);define_type_check(reverse_iterator);define_type_check(const_reverse_iterator);define_type_check(const_pointer);define_type_check(const_reference);define_type_check(pointer);define_type_check(reference);define_type_check(value_type);define_type_check(difference_type);define_type_check(size_type);// Okay this example works as expected// just prints 1 for true that std::vector has a type named value_type//#include <iostream>//#include <vector>//#include <boost/type_traits.hpp>//#include "container_traits.hpp"////int main ()//{// typedef std::vector<int> vec;// std::cout << tmp::has_value_type<vec>::value << std::endl;// return 0;//}// And works fine, until I uncomment any one or all of the following lines. Then it starts printing 0's// for the same problem. I don't see whats going on. It still compiles fine, but doesnt print// the correct value any more.//define_type_check(key_compare); // <-Uncommenting this code line and the program prints the wrong result//define_type_check(key_type); // <-Uncommenting this code line and the program prints the wrong result//define_type_check(mapped_type); // <-Uncommenting this code line and the program prints the wrong result//define_type_check(value_compare); // <-Uncommenting this code line and the program prints the wrong result//define_type_check(container_type); // <-Uncommenting this code line and the program prints the wrong result// THis is very strange to me. It seems so random.} // end namespace tmp#endif HPP_CONTAINER_TRAITS_03152007Quote:
Oh, can someone tell me how to view my code after the preprocessor has made its substitutions? I hear of people doing it all the time but i never seen than option in any IDE i've used. Im using VS2005 right now.
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more