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
8
votes
1 answer

pointer to function and ODR

There are so many questions on ODR but I cannot find what I'm looking for, so apologies if this a duplicate or if the title is inappropriate. Consider the following: struct t {t(*id)();}; template t type() {return {type};} This is…
iavr
  • 7,547
  • 1
  • 18
  • 53
7
votes
1 answer

Non-type function template parameters

I am reading C++ Templates Complete Guide and came across this non-type function template parameters code (I have added the main() and other parts except the function definition and call): #include #include #include…
badmaash
  • 4,775
  • 7
  • 46
  • 61
7
votes
0 answers

Linker error: undefined reference in a function template that uses external variables

I have a mini project with two files: main.cpp #include template int getint(int i) { extern std::string var; return var.size() * i; } int main() { return getint<2>(2); } and sub.cpp #include extern std::string…
7
votes
3 answers

Deduction failure of function call with explicit template argument list and [temp.arg.explicit]/3

[temp.arg.explicit]/3 of the C++17 standard (final draft) says about deduction of function template arguments with explicitly specified template argument lists: In contexts where deduction is done and fails, or [...], if a template argument list is…
walnut
  • 21,629
  • 4
  • 23
  • 59
7
votes
1 answer

Specify return type based on other template argument

I'd like to specify my templated function's return type, by an other template argument. All of this inside a class. In the header file: class MyClass { template RT myfunc(); }; In the .cpp something like…
Sipka
  • 2,291
  • 2
  • 27
  • 30
7
votes
1 answer

C++ about generic initialization in templates

I am writing a generic function like below. template void foo(Iterator first, Iterator last) { T a; cout << a << endl; // do something with iterators } typedef vector::iterator DblPtr; vector
6
votes
3 answers

Why do two functions have the same address?

Consider this function template: template unsigned long f(void *) { return 0;} Now, I print the addresses of f and f as: std::cout << (void*)f << std::endl; std::cout << (void*)f << std::endl; Why do they print the same…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
6
votes
2 answers

Wrapping C++ template function

I try to wrap a template function with the help of GNU's linker wrap option. The code looks like this: // f.h template void f(T t) { } // bar.h void bar(); // bar.cpp #include "bar.h" #include "f.h" void bar() { f(42); } //…
Mike
  • 187
  • 2
  • 8
6
votes
2 answers

How to constraint a template to be iterable ranges using concepts?

Say I have some template function that returns the median of some iterable object passed into it. Something like: template decltype(auto) find_median_sorted(T begin) { // some code here } Now I want to make sure I constrained T to…
beep_boop
  • 819
  • 6
  • 15
6
votes
1 answer

C++20 std::source_location yield different column numbers between free function and template functions

Consider the template function g() and free function f(): #include #include auto g(auto...) { std::cout << std::source_location::current().column() << "\n"; } auto f() { std::cout <<…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
6
votes
1 answer

Flutter create Generic Function with T extending an interface

Context In my code, I have an interface called AbstactDataModel that is used as a starting point for all the data model classes. This is implemented so that i know that whatever xyzModel class I may need, it will have a fromJson(Map
Mike
  • 135
  • 1
  • 9
6
votes
1 answer

How strictly function template explicit instantiation rules are defined?

Note: this question is about explicit instantiation, not explicit specialization. Please take a look at the following example: template void f (X &x) {} // 1 template void f (int &x) {} // 2 template void f (int…
6
votes
2 answers

Should this function call be ambiguous?

I stumbled on this the other day and can't figure out which answer is correct, or whether both are acceptable. Specifically, I'm referring to the call to bar(T{}) in OtherFunction. From what I've been able to test on compiler explorer, the decision…
6
votes
3 answers

Inconsistency in function type decay between variadic/non-variadic templates?

Given a non-variadic function template: template void f(void(t)(T)); And some plain functions: void f1(int); void f2(char); This works: f(f1); The type of t becomes void (*)(int). However, the variadic counterpart: template
Jamboree
  • 5,139
  • 2
  • 16
  • 36
6
votes
2 answers

Argument-dependent lookup and function templates

Here is an example: #include #include #include using std::string; int main() { string str = "This is a string"; // ok: needn't using declaration, ADL works auto it = find(str.begin(), str.end(), 'i'); …
1 2
3
26 27