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

std::to_array for multi dimensional array

C++20 added std::to_array so you can easily create std::array from a C-Style array, for example: template void foo(const T (&a)[N]) { auto arr = std::to_array(a); } But, std::to_array does not support two-dimensional…
Amir Kirsh
  • 12,564
  • 41
  • 74
12
votes
2 answers

Can I use (boost) bind with a function template?

Is it possible to bind arguments to a function template with (boost) bind? // Define a template function (just a silly example) template ARG1 FCall2Templ(ARG1 arg1, ARG2 arg2) { return arg1 + arg2; } // try to bind…
Martin Ba
  • 37,187
  • 33
  • 183
  • 337
10
votes
3 answers

function template specialization failed?

#include template void foo(T) { std::cout << "foo(T)" << std::endl; } template void foo(T*) { //#3 std::cout << "foo(T*)" << std::endl; } #define TEST #ifdef TEST template <> void foo(int*) { //#1 …
Xiaotian Pei
  • 3,210
  • 21
  • 40
10
votes
1 answer

Clang: Template deduction failed 'double' vs ''

Consider the following code, which uses a function with variable arguments: #include // Typedef function type template using Func = void(Output*...); // Function runner template void…
WaelJ
  • 2,942
  • 4
  • 22
  • 28
10
votes
1 answer

Is a friend function template defined in the class available for lookup? clang++ and g++ disagree

Here is the code: struct foo { template friend foo f() { return {}; } }; int main() { auto x = f(); // clang++ can't find it, g++ can. } clang++ 3.4 gives: fni2.cpp:8:12: error: use of undeclared identifier 'f' auto x =…
kec
  • 2,099
  • 11
  • 17
9
votes
2 answers

Implementing variadic Max function in C++20

Despite, the fact, we have std::max, I wanted to try if it is possible to make a Max version that takes variadic arguments and calls the Max recursively for finding the max element. I saw similar posts in stack overflow, but those are old and most…
MyClass
  • 322
  • 3
  • 10
9
votes
5 answers

How to return correct type of data in templates?

#include using namespace std; template Y big(X a, Y b) { if (a > b) return (a); else return (b); } int main() { cout << big(32.8, 9); } Here I am using templates in CPP, so when I call the function big…
9
votes
5 answers

partial specialization of function templates

In the below code snippet, template void func(T1& t) { cout << "all" << endl; } template void func(T2 &t) { cout << "float" << endl; } // I do not want this // template<> void func(float &t) int main() { int…
josh
  • 13,793
  • 12
  • 49
  • 58
9
votes
1 answer

friend function template lookup

According to the standard, friend function declared and defined in class can only be find by ADL. So, I think the following code should compile. template struct test{ template friend void foo(test){} }; int main(){ …
8
votes
2 answers

Why do we need voidify function template in uninitialized_copy

I was reading about std::uninitialized_copy and came across something called voidify: Effects: Equivalent to: for (; first != last; ++result, (void) ++first) //#1----vvvvvvv----------->what does this call to voidify mean here ::new…
Kal
  • 475
  • 1
  • 16
8
votes
3 answers

Passing a templated function as method argument without lambdas?

I did like to being able to use extFunction or std::max or std::min as argument for the square method without declaring a lambda : template T extFunction(T a, T b) { return a; } class Stuff { public: template
8
votes
3 answers

Friend functions of a class template

I have a class template Foo. I'd like to implement a non-member function Bar that takes two Foos and returns a Foo. I want Bar to be a non-member because it will be more natural for callers to write Bar(f1, f2) than f1.Bar(f2). I also want Bar…
Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
8
votes
1 answer

Passing (partially) templated template function as std::function(or function pointer)

#include #include template class Foo { public: template void std_function(std::function)> functor) { /* something */ } template void func_ptr(F…
suhdonghwi
  • 955
  • 1
  • 7
  • 20
8
votes
1 answer

constexpr static template function: g++ error is a warning on clang

Consider the following snippet: #include template constexpr int f() { return I * f(); } template<> constexpr int f<0>() { return 1; } int main () { std::cout << f<5>(); return 0; } This code compiles nicely with both…
8
votes
2 answers

Why can't I use std::get<0> in std::transform?

In trying to compile the following code which would copy a maps keys to a vector: map mss; vector vs; transform(mss.begin(), mss.end(), back_inserter(vs), get<0>); VS2013 can't distinguish which get is intended but this…
screwnut
  • 1,367
  • 7
  • 26
1
2
3
26 27