C ++ template typedef as an argument to a template function

Why does the compiler not find a match for read1 ? I do not see the difference between read1 and read2 ; Is there a restriction for nested typedef templates like those found in the Foo class?

 template<typename T> class Handle{}; class Foo{ public: typedef Handle<Foo> Handle; }; template<typename T> void read1(typename T::Handle){} template<typename T> void read2(Handle<T>){} int main(int argc, char** argv) { Foo::Handle f1; read1(f1); Foo::Handle f2; read2(f2); } 

G ++ compiler output, (g ++ 4.4.5)

 g++ -c -I. main1.cpp main1.cpp: In function 'int main(int, char**)': main1.cpp:37: error: no matching function for call to 'read1(Handle<Foo>&)' 
+4
source share
2 answers
 Foo::Handle f1; read1(f1); 

The type passed to read1 is Handle<Foo> , not Foo.

Templates are not inherited. Handle<Foo> is a separate class that is not Foo, so there is no Handle<Foo>::Handle .

+4
source
 template<typename T> void read1(typename T::Handle) { } 

First, you can never call this function without providing an explicit template parameter, such as read1<Foo>(f1) . Read on SFINAE .

Secondly, how should the compiler ever know what T ? That would be to check all the nested typedefs of all the possible classes you could ever write. Sounds impossible? It.

+3
source

Source: https://habr.com/ru/post/1387662/


All Articles