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
5
votes
2 answers

Templated Functions.. ERROR: template-id does not match any template declaration

I have written a function template and an explicitly specialized templated function which simply takes in 3 arguments and calculates the biggest among them and prints it. The specialized function is causing an error,whereas the template works…
Pavitar
  • 4,282
  • 10
  • 50
  • 82
5
votes
3 answers

Template Function Selection Based on Nested Type

The following code works correctly on VS2015: struct Foo { using Bar = int; auto operator()() { return "Foo!"; } }; template // <<< CodeType is a template param void funky(CodeType code, Callable…
Adi Shavit
  • 16,743
  • 5
  • 67
  • 137
5
votes
2 answers

Function template overload resolution with a pointer argument

The following code demonstrates the core of a C++ template metaprogramming pattern I have been using to determine whether a type T is an instantiation of a specific class template: #include template struct…
5
votes
2 answers

Compilation issue with instantiating function template

Consider the following code: #include struct S { void f(const char* s) { std::cout << s << '\n'; } }; template void invoke(S* pd, Args... args) { (pd->*mem_fn)(args...); } int…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
5
votes
2 answers

In C++ 11, how do I specialize a function template that takes a function object based on return type?

I have a wrapper function in C++ 11, designed to be used with lambdas, like so: template int WrapExceptions(Func&& f) { try { return f(); } catch(std::exception) { return -1; } } And I can call it like so: int rc…
John Doty
  • 3,193
  • 1
  • 16
  • 10
5
votes
1 answer

Unexpected overload resolution with default function template parameter

I am experiencing an overload resolution behaviour that seems very unexpected. The following code is rejected with an ambiguity error by both gcc and clang: template struct A { typedef T key_type; }; template void…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
5
votes
3 answers

Why is the compiler not selecting my function-template overload in the following example?

Given the following function templates: #include #include struct Base { }; struct Derived : Base { }; // #1 template void f(const T1& a, const T2& b) { }; // #2 template
5
votes
3 answers

Template parameter default to a later one

This link doesn't answer my question so I'll ask it here: Basically I want to write a template function template Out f(In x); Here I always need to specify Out when calling f. I don't want to do it every time, so I…
Kan Li
  • 8,557
  • 8
  • 53
  • 93
5
votes
2 answers

Template function with iterators but fixed type

ProcessIndex( int index ); template< typename Iterator > void ProcessIndexes( Iterator start, Iterator end ) { while( start!=end ) { ProcessIndex(*start++); } } How can I enforce that this function can only ever be called with…
Dr.D.
  • 477
  • 3
  • 11
5
votes
3 answers

moving std::enable_if from parameters to template parameters

I am basically trying to do the same as std::enable_if : parameter vs template parameter but I can't get my code to compile. I had a simple first version that has the std::enable_if in the parameters and which works fine: #include…
Sarien
  • 6,647
  • 6
  • 35
  • 55
5
votes
1 answer

C++ Function Template instantiaion with implicit parameters

I can't figure out why the following code compiles fine: #include void bar(int x) { std::cout << "int "…
kirakun
  • 2,770
  • 1
  • 25
  • 41
4
votes
2 answers

How to avoid redefinition error in case of in-class definition of friend function template?

Consider this code: template class Base { template friend void f(void *ptr) { static_cast*>(ptr)->run(); } protected: virtual void run() = 0; }; class A : public Base { protected: …
Nawaz
  • 353,942
  • 115
  • 666
  • 851
4
votes
1 answer

Why does Stroustrup's book demonstrate default function template arguments, which weren't allowed at the time?

Can anyone explain me why in chapter 13 of the third edition of C++ Programming Language, Stroustrup illustrates default parameters for function templates, although they are not supported by C++ (pre C++11)? This is the example given by Stroustrup…
Martin
  • 9,089
  • 11
  • 52
  • 87
4
votes
0 answers

If I forward declare a function template, may I put the definition after the calling site and not explicit instantiate it at all?

In a header file of a large project, I have to forward declare a function template before the calling site. The code boils down to this: //H1.h #pragma once template void f(); inline void g() { f(); } //POI #1 template…
4
votes
1 answer

Usage of decltype in return type of function template removes error due to exception specification

I saw an answer to a question here. There the author of the answer made use of the fact that exception specifications do not participate1 in template argument deduction. In the answer linked above it is explained why the following doesn't…