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

Perfect forwarder in Herb Sutter's C++Con 2014 talk

In Herb Sutter's talk at C++Con 2014, among other things he discusses passing by value, by reference, and so forth. One technique he presents in this albeit contrived example is: using namespace std; class employee{ string name_; public: …
3
votes
4 answers

class T in c++ (your definition)

The one advantage of using class T in c++ is to reduce the time to redefine data types in a function, if those data types are defined in other function, for example, in int main. template void showabs(T number) { if (number < 0 ) …
CppLearner
  • 16,273
  • 32
  • 108
  • 163
3
votes
2 answers

Template function specialization symbol matching across libraries

So far, I had a setup where a certain function template getF was declared like this in the headers template F* getF(); leaving the function body undefined. Then on a shared library, getFhas some specializations.. template<> F*…
lurscher
  • 25,930
  • 29
  • 122
  • 185
3
votes
3 answers

How to: variadic wrapper function that catches exceptions of input function

I am trying to create a function that I can pass other functions, which will catch any errors, but otherwise simply return the return value of the function. Here's what I've tried: #include using namespace std; int fun(int input) { …
quant
  • 21,507
  • 32
  • 115
  • 211
3
votes
1 answer

Why does C++ instantiate a base template function which is masked by a full specialization?

Let's say I have a base template function foo(), with a full specialization for T = int. The body of the base template for foo() invokes a another template, Bar::baz, but foo() does not. If foo() is written, the compilation fails…
trbabb
  • 1,894
  • 18
  • 35
3
votes
4 answers

How to get an object of a unknown class with given classname

I am searching for a way to determine at runtime, which type of object should be alloced (based on a given class name, which is of type const char*). Well the simplest way of course is to use loads of ifs /else ifs, but that doesnt seem applicable,…
smerlin
  • 6,446
  • 3
  • 35
  • 58
3
votes
3 answers

Delayed function template instantiation

In this rather contrived example, I'm trying to pass a function template to my function, and want my function to instantiate the function template internally. In essence, I don't want the user to know the types, and workings of my function, but just…
Alexander Kondratskiy
  • 4,156
  • 2
  • 30
  • 51
3
votes
2 answers

Portability issue with template function Instantiation

I'm porting a project from MSVC to Borland C++ and I'm running into difficulties with template functions. For example, the following void fn(const char *buffer) { vector output; boost::split(output, string(buffer), is_any_of(",")); //…
3
votes
4 answers

Which of two overloaded templates will be called?

I am still trying to figure out templates. I have read about the specialization rules and don't understand what is happening here. I have defined the following in templates.h: #include template void f(foo p) { std::cout…
Sarien
  • 6,647
  • 6
  • 35
  • 55
2
votes
2 answers

Function template linking error

I've created a function template that allows me to get data for any data type but am receiving the error message on compilation: Undefined symbols for architecture i386: "bool Json::getData(double, Json&, std::basic_string
jdeckman
  • 119
  • 10
2
votes
1 answer

How to create an array of N floats values with fold expression?

Suppose the following function template constexpr std::array make_ones() { std::array ret{}; for (size_t k = 0; k != N; ++k) { ret[k] = 1.0f; } return ret; } Is it possible to write that…
user877329
  • 6,717
  • 8
  • 46
  • 88
2
votes
2 answers

What is the difference of using `typename` in the following?

What is the difference between using "typename" before the return type of a function and without using it at the declaration of a function like the following below? And what is different if we don't use it at all? template< class T > typename…
Crackie
  • 203
  • 6
2
votes
2 answers

Is there a way to pass a function template, as an argument in another function?

The best way for me to describe what I am asking is a simple example. template void execute_example(T* begin, T* end) { T val = 10 * 0.8; while (begin != end) { *begin = val; val *= 0.8; ++begin; …
Sam Moldenha
  • 463
  • 2
  • 11
2
votes
3 answers

How to define and use std::less as a template argument in a function definition?

What is the proper C++ syntax to supply and use std::less as a template argument? #include #include template float order(float a, float b) { return CMP(a, b) ? a : b; // COMPILE ERROR …
wcochran
  • 10,089
  • 6
  • 61
  • 69
2
votes
2 answers

Is it possible to return a member variable of a class specified by a template function?

I am trying to generalize a function for a game engine I am writing to simplify the shader loading process. Anyway, the difficulty arises in my attempts to templating the function. I attempted to adapt the solution found in this stack thread, and…