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

Overload function template using a templated type

I have a given function like this: template T function(const F& f) const; This function has various overloads, some of them concept based. Similar to the following: template T function(const F&…
0
votes
2 answers

How to pass normal param as well as template param in a template function in C++?

I have a template function (as follows) in a namespace called myNamespace: template void getRandomItems(NaturalNumber size, setX &random, setX &items) { assert(size <= items.size()); //set of randomly selected indices for…
0
votes
1 answer

How do I fix my program that calls a template function using parameters of different types?

I cannot figure out how I would call a template function that is suppose to take a string and College object as the parameters in my main.cpp. This is my template in LinkedListADT.h: #ifndef LINKED_LIST_H #define LINKED_LIST_H #include…
drewster
  • 83
  • 1
  • 8
0
votes
2 answers

Allow function of variadic argument count as template function argument

In C++ we can pretty easily define template function arguments as seen in this question. However, this only allows functions with a defined parameter list and return value. We can make those types template parameters like this: template
Reizo
  • 1,374
  • 12
  • 17
0
votes
1 answer

C++11 and after: How to store an object pointer and method pointer, received in a templatized method, for later use?

Question: I have a template function that takes an object pointer of the template param class, and a method pointer to a method of that class. I can then call that method on that object immediately. But I don't want to call it immediately. …
Swiss Frank
  • 1,985
  • 15
  • 33
0
votes
1 answer

C++11: template function call of a template method doesn't compile?

The following program, if compiled as shown, outputs size: 1 align: 1. However, trying to make the same method-template call from a templatized function doesn't work. If I change #if 0 to #if 1 g++ 9.2.1 gives me error expected primary-expression…
Swiss Frank
  • 1,985
  • 15
  • 33
0
votes
2 answers

Template function to return containers of unrelated types

I have several containers of unrelated types. I would like to write a templated function to process input values and insert them into the corresponding containers. This is my best try so far: #include #include struct A { // Must…
Walter Nissen
  • 16,451
  • 4
  • 26
  • 27
0
votes
1 answer

How to pass a overloaded function pointer with a template data type?

In the below code I want to create a function count which counts the number of integers/strings which qualifies a match criteria from a vector of integers/strings. But I am not clear about how to write the function definition. #include…
0
votes
0 answers

How to write a template repeating a block of code?

I can use any version of C++ (up to 20). Having a function implemented in assembly language as GCC's extended asm static inline void foo(){ __asm__ __volatile__("Some asm code" ::: "some clobbers"); } how to write a template which would…
Some Name
  • 8,555
  • 5
  • 27
  • 77
0
votes
2 answers

Can type arguments be made deduceable for function templates using std container?

I found this implementation of a few common features of functional programming, e.g. map / reduce: (I'm aware stuff like that is aparently coming or partially present in new C++ versions) github link A part of the code: template
sktpin
  • 317
  • 2
  • 15
0
votes
2 answers

C++: Using a template parameter as a default argument in a template function

Given the snippet of code below: template void function(int x=n){ double y=m; int array[n]; …. } void main () { function<1+2,2>(8); } when the function is compiled is x going to be 3 or 8 (as n is just the default parameter)?
wayta
  • 35
  • 7
0
votes
1 answer

is it legal for an auto variable in a function to represent a different type in different invocations?

Consider the following function: using iteratorPair = std::pair::const_iterator, std::set::const_iterator>; using iteratorList = std::vector; template iteratorList…
Nicolas Holthaus
  • 7,763
  • 4
  • 42
  • 97
0
votes
0 answers

lambda as part of a static variable in a template function

Let's say we define a simple struct S containing 2 members (1 int and 1 functor) in a header file. And we create a such struct as a static const variable in a template function foo and return the const reference of this struct. Is it legal in c++17?…
0
votes
3 answers

Why don't default parameters work alongside a parameter pack?

Because of 8.3.6 ([dcl.fct.default])/4, In a given function declaration, each parameter subsequent to a parameter with a default argument shall have a default argument supplied in this or a previous declaration or shall be a function parameter…
0
votes
1 answer

How to transfer a class type to a function template for new operation as a parameter?

I have a piece of c++ code: #include #include #include static counter = 0; class Probe { private: int supply_; Probe(const Probe&); public: Probe() { supply_ = 10000; } int get_supply() …
wangmyde
  • 77
  • 8