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

std::enable_if - custom type trait that handles types and a template

I have a function template, and a class template: template T testFunction(T argOne, T argTwo) { //Implementation } template , int> = 0> class testClass { //Implementation } I want to…
0
votes
1 answer

C++: Template Function taking rvalue reference overloads non-template function

Trying to run a non-generic function but it is overloaded by a template function. The problem is that they take rvalue references as arguments. Here's what it kinda looks like: #include using namespace std; template void…
AymenTM
  • 529
  • 2
  • 5
  • 17
0
votes
0 answers

Why is this function template not working in hpp files?

Originally this works (file init.cpp): #include "main.h" #include "api/llemu.h" #include "api/lcd.hpp" template void lcd_print(std::int16_t line,std::string fmt,Args ...args){ const char* format = convert(fmt,args...); if…
Eden Cheung
  • 303
  • 2
  • 8
0
votes
2 answers

How to declare function with argument that is a closure in C++11 in a Cuda device function?

I'm working on Cuda with C++11 (I don't think Cuda supports later C++ versions yet). I've a closure object that is passed to the function Process() which calls the closure for each iteration. I understand that std:: functionality is generally not…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
0
votes
1 answer

template function parameter pack not at the end of the list

The following code fails to compile both on g++ and clang++ with different error messages. In particular, it is the second line of main which triggers an error. I do not understand why, as there is nothing ambiguous. The function has two arguments,…
Fabio
  • 2,105
  • 16
  • 26
0
votes
0 answers

Problem using template specialization in headers

I am implementing my templates in a header file "ival.h". My problem is that this is causing some problem with a template specialization. Are template specializations implemented in .cpp rather than in headers as templates are? I have the following…
Daniel Duque
  • 159
  • 9
0
votes
0 answers

Subscript operator and implicit conversion to a pointer type

I'm reading C++ Templates - The Complete Guide, 2nd Edition, and B.2.1 tells about implicit conversion of the implied "this" argument. Same example here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1592.pdf Depending on the typedef of…
0
votes
2 answers

How to create a template function that takes a key as input and finds a value in a C++ std:map?

I have two std:map where one's key is of type CustomClass1 and another's key is of type CustomClass2, but their value's type is the same, std::string. //first map std::map //second map std::map
zfgo
  • 195
  • 11
0
votes
3 answers

Function Template with optional argument or overloaded

I got a class, containing 20 structure elements in a classical C-Array. The elements form 0 to 5 belong to Type A, from 6 to 15 they belong to Type B and the rest belongs to Type C. For looping this elements, I designed three function templates.…
Jan021981
  • 521
  • 3
  • 28
0
votes
1 answer

Is it possible to invoke an injected friend template function?

To be consistent with other (non-template) functions in a class I wanted to define and invoke a friend template function. I can define it with no problem (see function t below). namespace ns{ struct S{ void m() const{} friend void f(S…
alfC
  • 14,261
  • 4
  • 67
  • 118
0
votes
1 answer

c++ Using a a template function with enum class and overloaded conversion operators

I was reading the sample code on another post Specializations only for C++ template function with enum non-type template parameter and I'm trying to take it one step further, by using a overloaded conversion operator to use the object as if it was…
0
votes
1 answer

Function mismatch between vectors with identical stored types

I have define a template function called "Create2DBBox" just for create bounding boxes from a point cloud vector, the implementation details are less important. I want to use the template PointT type for accepting different Point type such as…
0
votes
0 answers

Correct code structure for class/function templates?

Let's say we have a class with a function template. Header file Sort.hpp with the class definition: #ifndef TEST_SORT_HPP #define TEST_SORT_HPP #include using namespace std; class Sort { public: template static void…
Snackoverflow
  • 5,332
  • 7
  • 39
  • 69
0
votes
0 answers

full function template specialization of operator/ from std complex not working because clang thinks I want to make a partial specialization

Due to a very very long list of constraints and gotchas... in a special math library that uses libc++ (and in other cases, libstdc++), which I am trying to extend, I am attempting to fully specialize just the division operation because in libc++ it…
Dank
  • 1
0
votes
1 answer

"Builder" for function template

Consider the following template function: template int foo(/* skipped */) { ... } The key is that it is has a long list of template paramters, both type and non-type,…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386