Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct format for separating declaration and definition of C++ template functions

I'm using this method http://www.parashift.com/c++-faq-lite/separate-template-fn-defn-from-decl.html to separate the definition of a C++ template function from its declaration, to avoid cluttering up my header files with code.

The link uses as an example a function with no arguments or return, but suppose I have a function with an argument. The link would suggest the following arrangement:

// File "f.h" 
template <typename T> void f(T t);

// File "f.cpp"
#include "f.h"

template <typename T> void f(T t) {
  // do something
}

template void f<int>(int t);
// other specializations as needed

However it seems the specialization also works if you omit the type in angle brackets, as I suppose the compiler deduces it from the argument type:

template void f(int t);

But I'm wondering, is it valid to do that?

Visual C++ 12 (2013)

like image 217
George Skelton Avatar asked Jun 02 '14 18:06

George Skelton


People also ask

How do you declare a template function?

To instantiate a template function explicitly, follow the template keyword by a declaration (not definition) for the function, with the function identifier followed by the template arguments. template float twice<float>( float original ); Template arguments may be omitted when the compiler can infer them.

What is the correct form of declaring a template in C++?

Class Template Declarationtemplate <class T> class className { private: T var; ... .. ... public: T functionName(T arg); ... .. ... }; In the above declaration, T is the template argument which is a placeholder for the data type used, and class is a keyword.

Which of the below options is correct in defining a template for a function?

What is the correct syntax of defining function template/template functions? Explanation: Starts with keyword template and then <class VAR>, then use VAR as type anywhere in the function below.

What is template What is the need of template declare a template class?

Templates in c++ is defined as a blueprint or formula for creating a generic class or a function. Generic Programming is an approach to programming where generic types are used as parameters in algorithms to work for a variety of data types.In C++, a template is a straightforward yet effective tool.


1 Answers

Yes, it's valid. [temp.explicit] p3 says:

If the explicit instantiation is for a function or member function, the unqualified- id in the declaration shall be either a template-id or, where all template arguments can be deduced, a template-name or operator-function-id.

Your function has one template parameter and it can be deduced from the function argument, when the function parameter is int the template argument can be deduced as int, so you can (optionally) use the template-name, f, instead of the template-id f<int>.

like image 187
Jonathan Wakely Avatar answered Oct 19 '22 17:10

Jonathan Wakely