c++ - Error when calling an Integral template member function with g++ and clang++ -


this question has answer here:

i'm stuck on compilation error, can't identify...

here's minimal working example:

#include <iostream>  template <typename t, int r> class a_type { public:      template <int n>      double segment()       {            return 42;       } };  template <int m> double func() {      a_type<double, m> a;      return a.segment<1>(); }  int main(int argc, char *argv[]) {      std::cout << func<10>() << std::endl;      return 0; } 

the error message gcc reads:

g++ main.cpp -o main main.cpp: in function 'double func()': main.cpp:18:26: error: expected primary-expression before ')' token       return a.segment<1>();                           ^ main.cpp: in instantiation of 'double func() [with int m = 10]': main.cpp:23:28:   required here main.cpp:18:22: error: invalid operands of types '<unresolved overloaded function type>' , 'int' binary 'operator<'       return a.segment<1>();                   ^ 

clang says similar:

clang++ main.cpp -o main main.cpp:18:26: error: expected expression      return a.segment<1>();                          ^ 

so based on gcc's error message, 'a.segment' member function call missing parentheses, gets rejected. not make sense @ all, since don't see reason treating expression such. moreover, if change m integral number on line 17, so:

#include <iostream>  template <typename t, int r> class a_type { public:      template <int n>      double segment()       {            return 42;       } };  template <int m> double func() {      a_type<double, 58> a;      return a.segment<1>(); }  int main(int argc, char *argv[]) {      std::cout << func<10>() << std::endl;      return 0; } 

then code compiles , produces expected result.

i happy if enlighten me , show me missing here.

the compiler doesn't know a.segment template (it might depend on value of m). have tell it:

return a.template segment<1>(); 

in second example knows type of a, , there no problem.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -