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

type deduction failing for auto stdMaxInt = std::max;

Using GCC 4.8.4 with g++ --std=c++11 main.cpp outputs the following error error: unable to deduce ‘auto’ from ‘max’ auto stdMaxInt = std::max; for this code #include template const T& myMax(const T& a, const T& b) { …
6
votes
2 answers

Function template specialization and the Abrahams/Dimov example

(I'm assuming knowledge of the Abrahams/Dimov example in this question.) Assume there is some 3rd-party code in a header that like this, which you cannot modify: template void f(T); // (1) base template 1 template void f(T *); …
user541686
  • 205,094
  • 128
  • 528
  • 886
6
votes
4 answers

Function templates: Different specializations with type traits

Considering class templates, it is possible to provide template specializations for certain types of groups using type traits and dummy enabler template parameters. I've already asked that earlier. Now, I need the same thing for function templates:…
gexicide
  • 38,535
  • 21
  • 92
  • 152
5
votes
2 answers

C++ 11 conditional template alias to function

In C++ 11, I want to make a template alias with two specializations that resolve to a different function each. void functionA(); void functionB(); template using Loc_snprintf = functionA; template<> using Loc_snprintf =…
Adrián
  • 45
  • 1
  • 3
  • 12
5
votes
2 answers

Specialization of variadic template function over the non-variadic arguments

I have a template for a function that accepts at least one parameter and performs some formatting on the rest: template void foo(T first, ARGS&&... args) { // ... } I want it to do a different thing when the first…
5
votes
1 answer

MSVC vs Clang/GCC bug during overload resolution of function templates one of which contains a parameter pack

I was using parameter pack when I noticed that one such case(shown below) compiles fine in gcc and clang but not in msvc: template void func(T a, T b= T{}) { } template void func(T a, S... b) { } int main() { …
Jason
  • 36,170
  • 5
  • 26
  • 60
5
votes
2 answers

How to remove this kind of duplication (for cycle over types)?

I have code like this: template void registerCmd() { Command x{}; // do something with x... } namespace Cmd { struct GET { /* some methods */ }; struct GETSET { /* some methods */ }; struct DEL { /* some methods…
Nick
  • 9,962
  • 4
  • 42
  • 80
5
votes
1 answer

Does "used as non-type template parameter" make a function template implicitly instantiated?

I want to write a class template M that accepts incomplete type C as template parameter. But I also want C has some traits when it is eventually defined. Is this code guaranteed to compile if defined(FLAG), and to fail the compiling if…
5
votes
0 answers

constexpr function only works if declared as a seemingly irrelevant template

The following code doesn't compile; g++ 7.3.0 with --std=c++17 gives the error message invalid return type 'const C' of constexpr function 'constexpr const C operator+(const C&, int)' note: 'C' is not literal because 'C' has a non-trivial…
TonyK
  • 16,761
  • 4
  • 37
  • 72
5
votes
1 answer

D function templates and type inference

Consider the following code: module ftwr; import std.regex; import std.stdio; import std.conv; import std.traits; S consume (S) (ref S data, Regex ! ( Unqual!(typeof(S.init[0])) ) rg) { writeln (typeid(Unqual!(typeof(S.init[0])))); auto m…
vines
  • 5,160
  • 1
  • 27
  • 49
5
votes
1 answer

How to understand the rules of partial ordering about T& and T const&

template void show(T&); // #1 template void show(T const&); // #2 int main() { int a = 0; show(a); // #1 to be called } I'm confused about these partial ordering rules. Here are some quotes:…
5
votes
2 answers

Legal use of non-trailing function template parameter pack?

While debugging someone's nasty macro-generated code, I saw that MSVC++ alone was not happy with something akin to the following function template declaration: template void foo(); Fair enough. I was puzzled though as to…
5
votes
3 answers

Overloading the End of Recursion for a Variable Length Template Function

François Andrieux gave me a good workaround for this Visual Studio 2017 problem. I was trying to build on his answer like so: template ostream& vector_insert_impl(ostream& lhs, const char*, const T& rhs) { return lhs <<…
5
votes
2 answers

Function template with return type T doesn't compile

The following code compiles fine: template void f(const T &item) { return; } int main() { f("const string literal"); } Compilation succeeded at ideone : http://ideone.com/dR6iZ But when I mention the return type, it doesn't…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
5
votes
5 answers

How can I write a function template that can accept either a stack or a queue?

I'm implementing four algorithms that are completely identical except for what data structure they use — two use priority_queue, one uses stack, and the last uses queue. They're relatively long, so I'd like to have just one function template that…
Joel McCance
  • 167
  • 1
  • 8