Using C ++ 0x:
namespace { template<class T> struct Template { }; } typedef Template<int> Template; #include<iostream> template<typename T> void PrintType() { std::cout << __PRETTY_FUNCTION__ << std::endl; } template<typename FullType, typename NewParameter> class Rebind { template<template<class> class Template, typename OldParameter> static Template<NewParameter> function(Template<OldParameter>); public: typedef decltype(function(FullType())) NewType; }; int main() { PrintType< ::Template>(); PrintType<Rebind< ::Template, float>::NewType>(); return 0; }
With gcc45 which gives
void PrintType() [with T = <unnamed>::Template<int>] void PrintType() [with T = <unnamed>::Template<float>]
It seems to be compiling with Cormeau, but I only have access to their online test, so I got stuck believing that it was functioning as expected.
I could not figure out how to pass the actual type into the structure directly and break it down into a template type, but the compiler had no problem decoupling them when it was necessary to guess the parameters of the function. Maybe this works in C ++ 03 using boost::result_of instead of decltype , but I never used it before, so I decided that I would stick to what I know.
Pay attention to the interval between main . Rebind<::Template, float>::NewType gets a gobbled parser because <: is a digraph. I think it turns into Rebind[:Template, float>::NewType . Thus, the space before ::Template vital.
As an aside, I had no idea that the nested template parameters cannot use typename [ template<template<typename> class T> , and not template<template<typename> typename T> ]. I think I'm relearning that every time I try to remember the syntax for a construct.
source share