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

Function pointers to template functions

I have the following C++ code which compiles: A function: template T funcC(int a, double b) { return a + b; } Then in main, I do the following: int (*anythingInt)(int,double); double (*anythingDouble)(int,double); anythingInt =…
2
votes
1 answer

How to modify each element of a parameter pack, and create a tuple from those?

I'm running into an issue with a variadic function template. I need to examine each element of a parameter pack, package the element, then stuff all the packaged elements into a tuple and return that. Here's the general idea of what I'd like to do…
Brett Rossier
  • 3,420
  • 3
  • 27
  • 36
2
votes
1 answer

operator== compiles with msvc but not with gcc and clang

I am learning C++ using the books listed here. Now, to further check that I've understood the concepts I'm also writing simple sample programs. One such program that compiles with msvc but does not compile with clang and gcc is given below.…
2
votes
1 answer

Function template argument deduction works in gcc but not in msvc and clang

I am learning C++ templates using the resource listed here. In particular, read about template argument deduction. Now, after reading, to further clear up my concept of the topic I tried the following example that compiles in gcc but not in clang…
Kal
  • 475
  • 1
  • 16
2
votes
3 answers

Are explicit template instantiation definition for a function template allowed in header files

I was reading about explicit template instantiation when i came across the following answer: Assuming by "explicit template instantiation" you mean something like template class Foo; // explicit type instantiation // or template void…
2
votes
2 answers

Why the std::is_array in template function is not distinguishing between int and array type?

In the following code, I am using a template function and type traits to distinguish between an integer type (else case) and array type. I would expect the output to be int and array respectively, instead I get int int with the two calls that…
roschach
  • 8,390
  • 14
  • 74
  • 124
2
votes
2 answers

How to conditionally get a `T` or `const T&` from template type `T`?

I want to have my template function print(The actual cenario is more complex template function) to have either a const T& or just T(pass by value), depending up on the characteristics of type T. In other words, if a type T is easily copyable (i.e.…
Const
  • 1,306
  • 1
  • 10
  • 26
2
votes
1 answer

Point of Instantiation for function templates

I am learning about templates in C++. In particular, i read about POIs. So i am trying out(by reading and writing) different examples. One such example which is not clear to me is given below: template void g1(T p) { } template
Jason
  • 36,170
  • 5
  • 26
  • 60
2
votes
3 answers

Template function cannot recognize lambda referred by an auto variable

In c++17, I have a template function which takes some kind of lambda as input. However it only recognizes those with explicit types and ones using auto are rejected. Why this is the case and any way to combine auto variables and template function…
2
votes
1 answer

Template member function specialization of a templated class without specifying the class template parameter

What is the correct syntax to specialize a templated member function of a templated class without specifying the class template parameter? Here is what I mean: Example #1 (works): #include struct C1 { template void f(void)…
2
votes
1 answer

How to get function template taking invokables to match the types?

I have the following code intended to take a generic function object that takes two arguments and return a function object that does the same with the arguments in the other order. #include #include template
MatthewJohnHeath
  • 393
  • 2
  • 12
2
votes
2 answers

How do I combine two template functions that have the same code?

I am trying to create a template for easily outputting containers, for the purpose of debugging code. I would like to be able to do int a = 3; pair b = {2,3}; vector c = {1,2,3,4}; map m; m[2] = 3; m[3] =…
2
votes
1 answer

Function template accepting either a template value parameter or a function argument

question Is there a way to write a templated function accepting either a template value parameter (for statically known values), or either a classic function argument (for dynamic values)? I would imagine something callable like this, allowing the…
Mat
  • 63
  • 6
2
votes
1 answer

Template with fold expression creates unwanted parameter copy

The following prototype is intended to make synchronized print: #include #include #include #include #include // for OutputDebugString std::mutex sync_mutex; template void…
Alex F
  • 42,307
  • 41
  • 144
  • 212
2
votes
2 answers

How to realize template class type in template function?

I want to design a print function for STL container, include: std::vector, std::map, std::unodered_map, std::set, std::unordered_set, std::list .... The ideal function looks like: template void Show(const T& t) { if (istype(t,…
nick
  • 832
  • 3
  • 12