Why redeclaring a template member function is allowed in C++?

class test{
 public:
 int call();
};

int test::call();

int main(){return 0;}

Above will not compile with this error:

error: declaration of 'int test::call()' outside of class is not definition 

However, the same code is allowed if templates are used as such:

class test{
 public:
 template<class T>
 int call();
};

template int test::call<int>(); // what's the point of this line?

template <class T>
int test::call(){
  T in = 5;
  return in;
}

int main(){
 test t;
 
 return 0;
}

I have 2 questions on this:

1- What's the point of the re-declaration? I have seen similar code in other projects.

2- Why does it compile and my first code snippet won't?



Read more here: https://stackoverflow.com/questions/68461857/why-redeclaring-a-template-member-function-is-allowed-in-c

Content Attribution

This content was originally published by Dan at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: