开发者

Template member function in regular class in template class does not compile with GCC but does with VC++

开发者 https://www.devze.com 2023-03-10 22:05 出处:网络
I am porting a C++ template library from VC++ to GCC. The example below compiles fine with VS2005 #include <iostream>

I am porting a C++ template library from VC++ to GCC.

The example below compiles fine with VS2005

#include <iostream>

template<class T>
struct A{
  struct B{
    template<int n>
    T foo() const{
      return static_cast<T>(n)/10;
    }
  };

  T bar() const{
    B b;
    return b.foo<2>();
  }
};

int main(){
  A<float> a;
  std::cout << a.bar() << std::endl;
  return 0;
}

but with GCC 4.4.1 I get the following errors

g++ ttest.cpp -o ttest
ttest.cpp: In member function ‘T A<T>::bar() const’:
ttest.cpp:14: error: expected primary-expression before ‘)’ token
ttest.cpp: In member function ‘T A<T>::bar() const [with T = float]’:
ttest.cpp:20:   instantiated from here
ttest.cpp:14: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘int’ to binary ‘operator<’
make: *** [ttest] Error 1

Could someone please explain to me what is wrong.

Is this compliant with the standard o开发者_JAVA百科r not?


You are missing a keyword, when invoking foo - which is a function template, you have to explicitly tell GCC it's a template, with the following horrible syntax (don't ask me why they thought it was sensible).

return b.template foo<2>();

EDIT: I'm not a language lawyer, so can't tell you whether it's standards compliant or not, I'm sure one of the many on this site can! ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消