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
3
votes
2 answers

How I can specialize template when a method is available?

I wonder how I can have a specialized template for when my type has a specific method. Let's take the following code as an example: template void foo(T&& arg, Args&&... args) { std::cout << "called…
Afshin
  • 8,839
  • 1
  • 18
  • 53
3
votes
1 answer

Template accepting all member function pointers (including CV-qualified and ref-qualified)

I want to write a template which accepts a pointer to a member function (possibly CV-qualified and/or ref-qualified) while also matching all the relevant types (return value's type, class type and types of the arguments). Simple version could look…
RippeR
  • 1,472
  • 1
  • 12
  • 23
3
votes
2 answers

Overloading function templates in namespace std

There was a discussion about function specialization here: Will specialization of function templates in std for program-defined types no longer be allowed in C++20? In principal I understand, that it is better to overload instead of specialize. But…
krzikalla
  • 73
  • 3
3
votes
2 answers

In function template, how to determine type of one argument based on another

I'd like to implement a function template that takes two arguments, a T* and a T, but where the second argument's type is determined by the first. Here's a minimal non-working example: #include #include #include…
user3188445
  • 4,062
  • 16
  • 26
3
votes
2 answers

function template parameter deduction of template parameter vs of default template parameter vs of return type

This is a question about how template deduction works when template parameter used as template parameter vs as default template parameter vs as return type. 1: plain template parameter As tested using GCC and VS compiliation of below snippet fails…
3
votes
3 answers

Most terse and reusable way of wrapping template or overloaded functions in function objects

Scenario 1: a template function pred template bool pred(T t) { /* return a bool based on t */ } Scenario 2: a set of functions overloaded on the same name pred bool pred(A t) { /* return a bool based on t */ } bool pred(B t) { /* return…
3
votes
1 answer

How to infer the template parameter from instantiation of function template?

I know how to infer the template parameter from an instantiation of a class template: template struct foo {}; template struct foo_param; template struct foo_param< foo > { using type = T; }; But I am…
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
3
votes
1 answer

C++ primer 5th ed function template specialization

Hello I have this code from C++ primer 5th ed: Primary function template: // first version; can compare any two types template int compare(T const& x, T const& y) { std::cout << "compare(T const&, T const&)\n"; …
Maestro
  • 2,512
  • 9
  • 24
3
votes
2 answers

How to write an insertion operator function template?

I'm trying to write a single function template instead of a bunch of similar overloads for the insertion operator. The redundant overloaded versions work, but when I try to unite them in a single function template, the compiler complains of…
3
votes
1 answer

Why no overload chosen on forward reference for function templates?

Snippet: #include template struct Printer{}; template void test(T&&) { std::cout << "Primary template called\n"; } template void test(Printer&&) { std::cout << "Specialized template…
Juergen
  • 3,489
  • 6
  • 35
  • 59
3
votes
2 answers

c++ member function template specialization on class non-type template parameter

Starting with the following example: #include template struct C { template void bar() {printf("Generic %d/%d\n",X,Y);} void foo() {bar();} }; int main(int argc, char *argv[]) { C<0,0> c0; c0.foo(); C<0,1>…
3
votes
2 answers

How do I convert vectors of various types to std::string?

I am coming to C++ from haskell and python where there are built-in ways of converting data types to strings. For example, in Haskell there is the polymorphic function show. I am interested in creating some template functions in C++ that would do…
composerMike
  • 966
  • 1
  • 13
  • 30
3
votes
2 answers

GCC C++14/17 Difference for Member Function Pointer Template Parameters

I have example code which compiles under C++14 fine on GCC/Clang/MSVC, and under C++17 on Clang/MSVC, yet under C++17 on GCC 8.x through 10.1 it produces an error. #include // vector template< typename Seq, typename…
Charles L Wilcox
  • 1,126
  • 8
  • 18
3
votes
1 answer

How to force, value to be lvalue reference?

I want my function to take a lvalue reference and absolutely not a rvalue or temporary or whatever. This is my function: template void foo(T& value) {} // Imagine I have a class Foo struct Foo { int a; int b; }; When I call…
Jojolatino
  • 696
  • 5
  • 11
3
votes
1 answer

template template parameter deduction: three different compilers three different behaviors

Consider the following demonstrative program. #include template struct A { }; template