Reputation: 6270
How to call a template function based on a dependent template alias of another template function?
For instance, there is a template class and a template function, which accepts this class as a template argument:
template<typename... T>
struct FooClass {
static int foo() { return 42; }
};
template<template<typename...> class T>
void printFoo() {
std::cout << T<>::foo() << std::endl;
}
Then, there is a class, which contains a template alias to FooClass
:
struct MyClass {
template<typename... U>
using FooAlias = FooClass<U...>;
};
Finally, there is another template function, which accepts MyClass
as a template argument and uses its FooAlias
to call printFoo
with an appropriate type:
template<typename T>
void lookUpAndPrintFoo() {
// printFoo<FooClass>(); //< works
// printFoo<MyClass::FooAlias>(); //< works
printFoo<typename T::FooAlias>(); //< does not compile
}
int main() {
lookUpAndPrintFoo<MyClass>();
return 0;
}
If I use the desired class directly or its alias, which does not depend on a template argument, everything is fine. As soon as I try to use a dependent alias, I get this error message:
prog.cpp: In instantiation of 'void lookUpAndPrintFoo() [with T = MyClass]':
prog.cpp:26:29: required from here
prog.cpp:22:32: error: 'typename MyClass::FooAlias' names 'template<class ... U> using FooAlias = struct FooClass<U ...>', which is not a type
printFoo<typename T::FooAlias>();
^
What is the proper way how to pass T::FooAlias
to printFoo()
?
Upvotes: 0
Views: 42