Why am I getting a "not pattern" f error used as a pattern?

I am trying to figure out where to use template and typename , and I ran into a problem that I cannot get around. I have a template function f<T> that uses the type passed to it (which will be a class) to call the template member function .f<T> . I think my use of typename in the body of the function is correct, however I continue to get the following error:

source.cpp: In the function 'void f()' :
source.cpp: 11:19: error: not pattern 'f' used as pattern
source.cpp: 11:19: note: use 'typename T::C::template f' to indicate that it is a template

 struct A { struct C { template <typename T> void f() {} }; }; template <typename T> void f() { typename T::C::f<int>(); } int main() { f<A>(); } 

Note that with the last error, it is recommended to use 'typename T::C::template f' . So I made the following change:

 // ... typename T::C::template f<int>(); // ... 

I did as he said, but then I got the following error line:

error: no class template named 'f' in 'struct A::C'

I believe that this error is incorrect in that there actually exists a public template function named f in struct A::C What can i do wrong here?

+4
source share
1 answer

Assuming we do f static , so it can be called without an instance, you do not need typename , because you do not create any ambiguities with a dependent type (i.e. C cannot be a variable, because you use it right after :: . Here is the correct syntax:

 struct A { struct C { template <typename T> static void f() {} }; }; template <typename T> void f() { T::C::template f<int>(); } 

If you do not want to statize it, you must create A::C and use .template f<int>() :

 struct A { struct C { template <typename T> static void f() {} }; }; template <typename T> void f() { typename T::C c; //^^^^^^^^ Now we DO need typename to disambiguate. c.template f<int>(); } 
+3
source

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


All Articles