Questions tagged [function-templates]

A function template behaves like a regular function except that works with types specified on the template arguments. The template arguments could be part of the function arguments and/or the function body. Each unique combination of template arguments will become an unique function when instantiated.

A function template by itself is not a type, or a function, or any other entity. No code is generated from a source file that contains only template definitions. In order for any code to appear, a template must be instantiated: the template arguments must be determined so that the compiler can generate an actual function (or class, from a class template).

Explicit instantiation

An explicit instantiation definition forces instantiation of the function or member function they refer to. It may appear in the program anywhere after the template definition, and for a given argument-list, is only allowed to appear once in the program.

An explicit instantiation declaration (an extern template) prevents implicit instantiations: the code that would otherwise cause an implicit instantiation has to use the explicit instantiation definition provided somewhere else in the program.

Implicit instantiation

When code refers to a function in context that requires the function definition to exist, and this particular function has not been explicitly instantiated, implicit instantiation occurs. The list of template arguments does not have to be supplied if it can be deduced from context

Template argument deduction

In order to instantiate a function template, every template argument must be known, but not every template argument has to be specified. When possible, the compiler will deduce the missing template arguments from the function arguments. This occurs when a function call is attempted and when an address of a function template is taken.

This mechanism makes it possible to use template operators, since there is no syntax to specify template arguments for an operator other than by re-writing it as a function call expression.

Template argument deduction takes place after the function template (which may involve ) and before .

Template argument substitution

When a template argument is specified explicitly, but does not match the type of the corresponding function argument exactly, the template argument is adjusted

Overload resolution

To compile a call to a function template, the compiler has to decide between non-template overloads, template overloads, and the specializations of the template overloads.

402 questions
380
votes
15 answers

Can a class member function template be virtual?

I have heard that C++ class member function templates can't be virtual. Is this true? If they can be virtual, what is an example of a scenario in which one would use such a function?
WannaBeGeek
  • 3,906
  • 3
  • 17
  • 9
155
votes
1 answer

Template function inside template class

I have this code: template class MyClass { public: template void foo() { U a; a.invoke(); } }; I want it in this form: template class MyClass { public: template void…
Michael
  • 2,356
  • 3
  • 21
  • 24
93
votes
4 answers

Why function template cannot be partially specialized?

I know the language specification forbids partial specialization of function template. I would like to know the rationale why it forbids it? Are they not useful? template void f() {} //allowed! template<> void f
61
votes
2 answers

Why is `const T&` not sure to be const?

template void f(T a, const T& b) { ++a; // ok ++b; // also ok! } template void g(T n) { f(n, n); } int main() { int n{}; g(n); } Please note: b is of const T& and ++b is ok! Why is const T& not…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
43
votes
2 answers

Template default argument loses its reference type

Consider #include #include template T foo(ARG_T v){ return std::is_reference::value; } int main() { int a = 1; std::cout << foo(a) << '\n'; std::cout <<…
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
40
votes
3 answers

Why is template constructor preferred to copy constructor?

#include struct uct { uct() { std::cerr << "default" << std::endl; } uct(const uct &) { std::cerr << "copy" << std::endl; } uct( uct&&) { std::cerr << "move" << std::endl; } uct(const int &) { std::cerr << "int"…
36
votes
2 answers

How to pass a template function in a template argument list

Suppose I have a template function: template T produce_5_function() { return T(5); } How can I pass this entire template to another template? If produce_5_function was a functor, there would be no problem: template struct…
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
28
votes
2 answers

Will specialization of function templates in std for program-defined types no longer be allowed in C++20?

Quote from cppreference.com: Adding template specializations It is allowed to add template specializations for any standard library |class (since C++20)| template to the namespace std only if the declaration depends on at least one program-defined…
Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
25
votes
4 answers

Partial ordering with function template having undeduced context

While reading another question, i came to a problem with partial ordering, which i cut down to the following test-case template struct Const { typedef void type; }; template void f(T, typename Const::type*) { cout <<…
24
votes
7 answers

Template function in C# - Return Type?

It seems that c# does not support c++ like templates. For example template myType GetMax (myType a, myType b) { return (a>b?a:b); } I want my function to have return type based on its parameters, how can i achieve this in c#? How to…
SMUsamaShah
  • 7,677
  • 22
  • 88
  • 131
23
votes
2 answers

Why does this template function not behave as expected?

I was reading about template functions and got confused by this problem: #include void f(int) { std::cout << "f(int)\n"; } template void g(T val) { std::cout << typeid(val).name() << " "; f(val); } void…
21
votes
4 answers

Variadic function template with pack expansion not in last parameter

I am wondering why the following code doesn't compile: struct S { template S(T..., int); }; S c{0, 0}; This code fails to compile with both clang and GCC 4.8. Here is the error with clang: test.cpp:7:3: error: no matching…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
20
votes
4 answers

C++ template specialization on functions

I'm playing around with template specialization, and I've found an issue I can't seem to solve; this is my code: template void test(T* array) { ... test(array); } template void test<0>(T*…
Skeen
  • 4,614
  • 5
  • 41
  • 67
15
votes
2 answers

Why default argument can't be added later in template functions?

C++ standard section 8.3.6.4 says that For non-template functions, default arguments can be added in later declarations of a function in the same scope. [...] But my question is that why it isn't allowed for template functions? What is the…
Destructor
  • 14,123
  • 11
  • 61
  • 126
15
votes
1 answer

Partial template function specification in C++ works, but why?

I'm trying to find out whether partial specification of templated functions is part of the C++ standard, or whether this is something compiler specific. By partial specification, I mean specifying only the types the compiler can't deduce. So if I…
1
2 3
26 27