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

Function template parameters failing to convert type during compilation

While trying to use a function template that calls a class's specialized static function template it is failing to convert its parameter from the template parameter list. Here is the function that I'm calling in main: template
0
votes
1 answer

C++11: Function template returning 2D containers of arbitrary types

I'm trying to solve a college problem, that, among other things, requires us to write a function which takes a parameter: container of Type1, that contains containers of Type2, which contain containers of Type3, which contain elements of an…
sishke
  • 43
  • 5
0
votes
1 answer

Variadic template argument deduction/substitution failed, metaprogramming

Im attempting to create a recursive template function, however I keep getting the error: no matching function for call to meuPrint(int&, Separator&, string&) while also receiving: candidate: template string meuPrint(U ...,…
0
votes
1 answer

C++: Issue with using Function Templates to Edit Array

So I have this assignment which requires me to use function templates to load array data from the keyboard, print the two smallest values in each array, sort the arrays in descending order, save the data to a text file, then retrieve that text…
0
votes
2 answers

error C2955: 'ListRemake' : use of class template requires template argument list

template class ListRemake { ... friend ostream& operator << (ostream& out, const ListRemake& obj); }; template ostream& operator << (ostream& out, const ListRemake& obj) { for (int i = 0; i < obj.size; i++) …
Caleb Jares
  • 6,163
  • 6
  • 56
  • 83
0
votes
2 answers

how to do specialize a function template when template parameter is a class template?

For example template T make() { return T(); } and I want to specialize it when T is a class template A; template class A {}; template A make>() { ... }; Error in compilation: illegal use of explicit template…
user1899020
  • 13,167
  • 21
  • 79
  • 154
0
votes
2 answers

template functions c++ invalid initialization of non-const reference of type

in my FTemplate.h, #ifndef FTemplate_h #define FTemplate_h_h template T& minus(const T& type1, const T& type2) { return type1 - type2; // error here } #endif in my main cpp #include #include int…
what
  • 373
  • 2
  • 10
  • 20
0
votes
1 answer

How to make std::distance a Callable for apply or invoke?

I played around with C++17/C++1z features apply and invoke a bit Ultimately I wanted to use apply to the result of equal_range directly like this: cout << apply(std::distance, data.equal_range(4)) << '\n'; That did not compile. I thought It was…
towi
  • 21,587
  • 28
  • 106
  • 187
0
votes
1 answer

C++ sibling template classes as template template parameters

I have a binary tree and I am trying to put each depth of nodes in its own linked list. I have the class templates: template class Node { public: T data; }; template class ListNode : public Node { …
moth
  • 226
  • 2
  • 9
0
votes
1 answer

Returning an iterator to graph node with function templates

I have 2 files, one is a .h file which contains my declarations and another is a .tem file which contains the implementation of my .h file. I'm having issues with the iterator for begin() and end() for my graph node iterator (shown below in "//…
iteong
  • 715
  • 3
  • 10
  • 26
0
votes
2 answers

Function template overloading (different data type validation function as parameters)

In its current state the program validates input for data types: int and string. //valid score range between 0 and 100 bool validScore(int value) { if(value < 0 || value > 100) return false; return true; } //valid name composed of…
andres
  • 301
  • 6
  • 21
0
votes
1 answer

Function Template Specialization Error

I am trying to specialize my function template for list of int pointers. template void sortowanie(typ *tablica, int rozmiar, Komparator *komparator) { int p; for(int j = rozmiar - 1; j > 0; j--) { p = 1; …
0
votes
1 answer

how accurate is find() and distance()

I think I read somewhere that distance() when returning the iterator position can be finicky. And sometimes it doesn't return the right position. I want to know if this is true or if I'm not using it right. I'm trying to find when a particle in a…
mauricioSanchez
  • 366
  • 10
  • 23
0
votes
1 answer

about passing a Predicate as template argument

I coded a generic function template like below that doesn't build. It builds only if the predicate is passeed by const reference or by value. Do you know what principle C++ is following in this case? I am using g++ and my version doesn't have C++11…
Abruzzo Forte e Gentile
  • 14,423
  • 28
  • 99
  • 173
0
votes
2 answers

template functions and classes

I'm really confused by templates. If I have a template class and I pass it as an argument to a function, consider the following : template class Class { }; So I want to make a function that takes Class as an argument, so why the…
Tugal.44
  • 153
  • 4
  • 13