
Hi, Is there a way to inspect if a type is a template instantiaton (is_templated) and the type of the instantiated template parameters (arg1_type)? is_templated< std::vector<int> >::value // true is_templated< int >::value // false template_traits< std::vector<int> >::arg1_type // int Thanks, --Lorenzo

On 09/11/2012 08:45 p.m., Lorenzo Caminiti wrote:
Is there a way to inspect if a type is a template instantiaton (is_templated) and the type of the instantiated template parameters (arg1_type)?
is_templated< std::vector<int> >::value // true is_templated< int >::value // false
template_traits< std::vector<int> >::arg1_type // int
I uploaded template traits to the vault a long time ago. You can find it at https://github.com/boost-vault/Miscellaneous as template_traits.zip, or simply https://github.com/boost-vault/Miscellaneous/blob/master/template_traits.zip It works only for templates of type arguments, no non-type or template arguments. There were other limitations, but I no longer remember them. Agustín K-ballo Bergé.- http://fusionfenix.com

AMDG On 11/09/2012 03:45 PM, Lorenzo Caminiti wrote:
Is there a way to inspect if a type is a template instantiaton (is_templated) and the type of the instantiated template parameters (arg1_type)?
is_templated< std::vector<int> >::value // true is_templated< int >::value // false
template_traits< std::vector<int> >::arg1_type // int
If you don't have any non-type template parameters or template template parameters, it's easy: template<class T> struct is_template : boost::mpl::false_ {}; template<template<class...> class T, class... U> struct is_template<T<U...> > : boost::mpl::true_ {}; template<class T> struct template_traits; template<template<class...> class T, class... U> struct template_traits { typedef typename boost::mpl::vector<U...> arg_types; }; In Christ, Steven Watanabe

Le 10/11/12 00:45, Lorenzo Caminiti a écrit :
Hi,
Is there a way to inspect if a type is a template instantiaton (is_templated) and the type of the instantiated template parameters (arg1_type)?
is_templated< std::vector<int> >::value // true is_templated< int >::value // false
template_traits< std::vector<int> >::arg1_type // int
Hi Lorenzo, The following works for c++98 and C++11. I left you rework the C++98 part to make it variadic with the help of the preprocessor ;-) Just before sending I saw that others were quicker than me :( Hoping this helps, Vicente #include <boost/type_traits.hpp> #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) template <typename T> struct is_templated : boost::false_type{}; template <template <class> class T, typename Arg1> struct is_templated<T<Arg1> > : boost::true_type {}; template <template <class, class> class T, typename Arg1, typename Arg2> struct is_templated<T<Arg1,Arg2> > : boost::true_type{}; // ... preprocessor template <typename T, std::size_t N> struct template_arg {}; template <template <class> class T, typename Arg1> struct template_arg<T<Arg1>,1> : boost::mpl::identity<Arg1>{}; template <template <class,class> class T, typename Arg1, typename Arg2> struct template_arg<T<Arg1,Arg2>,1> : boost::mpl::identity<Arg1>{}; template <template <class,class> class T, typename Arg1, typename Arg2> struct template_arg<T<Arg1,Arg2>,2> : boost::mpl::identity<Arg2>{}; // ... preprocessor #else template <typename T> struct is_templated : boost::false_type{}; template <template <class...> class T, typename ...Args> struct is_templated<T<Args...>> : boost::true_type {}; template <std::size_t N, typename ...Args> struct nth; template <typename Arg, typename ...Args> struct nth<1, Arg, Args...> : boost::mpl::identity<Arg> {}; template <std::size_t N, typename Arg, typename ...Args> struct nth<N, Arg, Args...> : nth< N-1, Args...> {}; template <typename T, std::size_t N> struct template_arg {}; template <std::size_t N, template <class...> class T, typename ...Args> struct template_arg<T<Args...>, N>: nth<N, Args...>{}; #endif // TEST #include <vector> #include <boost/static_assert.hpp> BOOST_STATIC_ASSERT((is_templated<std::vector<int> >::value )); BOOST_STATIC_ASSERT((is_templated<int>::value == false )); BOOST_STATIC_ASSERT((boost::is_same<template_arg<std::vector<int>, 1>::type, int>::value));

On Fri, Nov 9, 2012 at 6:45 PM, Lorenzo Caminiti <lorcaminiti@gmail.com>wrote:
Hi,
Is there a way to inspect if a type is a template instantiaton (is_templated) and the type of the instantiated template parameters (arg1_type)?
is_templated< std::vector<int> >::value // true is_templated< int >::value // false
template_traits< std::vector<int> >::arg1_type // int
I recently developed a way to do this in the backend of Boost.Generic that works even with non-type template parameters or combinations of types an non-types, etc., but it requires explicit registration of those kinds of "weird" template parameter lists, which might be unacceptable for your needs. -- -Matt Calabrese

On Fri, Nov 9, 2012 at 8:45 PM, Matt Calabrese <rivorus@gmail.com> wrote:
On Fri, Nov 9, 2012 at 6:45 PM, Lorenzo Caminiti <lorcaminiti@gmail.com>wrote:
Hi,
Is there a way to inspect if a type is a template instantiaton (is_templated) and the type of the instantiated template parameters (arg1_type)?
is_templated< std::vector<int> >::value // true is_templated< int >::value // false
template_traits< std::vector<int> >::arg1_type // int
I recently developed a way to do this in the backend of Boost.Generic that works even with non-type template parameters or combinations of types an non-types, etc., but it requires explicit registration of those kinds of "weird" template parameter lists, which might be unacceptable for your needs.
Thanks for all the replies. It turns out the solution to the question I originally asked doesn't help in the context of my original problem :( but thanks for telling me how to inspect template traits! --Lorenzo
participants (5)
-
Agustín K-ballo Bergé
-
Lorenzo Caminiti
-
Matt Calabrese
-
Steven Watanabe
-
Vicente J. Botet Escriba