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

Pass 'this' into function template, gives error

I try to pass to function template from other object the object this but it keep to give me compilation error. This is what my template function looks like: in header template void StopAndRemoveParticals(ParticleSystemQuad* &emitter,T*…
user63898
  • 29,839
  • 85
  • 272
  • 514
0
votes
2 answers

Variadic template function: argument number for each argument

I'm playing around with variadic function templates in C++11 and have got the basic idea with code something like: void helper() { std::cout << "No args" << std::endl; } template< typename T > void helper( T&& arg ) { size_t n = 0; …
user2746401
  • 3,157
  • 2
  • 21
  • 46
0
votes
2 answers

A function in the LLVM source that calculates array length

In llvm-3.4\include\llvm\ADT\STLExtras.h, i see this function: /// Find the length of an array. template inline size_t array_lengthof(T (&)[N]) { return N; } This function returns the array length: int main(){ const char…
0
votes
1 answer

Unknown function template parameter

I'm writing a application profiling library that basically hooks Windows APIs and records the parameters and results. I'm trying to come up with a way to generate these hooks in a manner using C++ templates that requires minimal effort to add new…
atanamir
  • 4,833
  • 3
  • 24
  • 20
0
votes
1 answer

How to make a template function specialization for a character array?

I'm trying to make a template function specialization for a bubble sort of character array's. For some reason though, when I'm about to define the function, I get an error underline over the function name and I have no idea why. template
onemic
  • 59
  • 1
  • 3
  • 10
0
votes
2 answers

Find Item Function Template Giving Me Problems

I am trying to find an item in a range so I have multiple tests for my templated function called "find". template T* find(T *left, T *end, T item); that is the function prototype I am using that fails to work with my first test which…
0
votes
1 answer

c++ error: no instance of function template

I'am trying to get a variable out of my config.lua file with c++. I've created a Lua-Class from a tutorial to get these variable but I'am getting an error when I try to call the function who gets the variable from config.lua here are the code…
chrisoo
  • 17
  • 1
  • 6
0
votes
1 answer

Default template parameter in Visual Studios 2012

This question is a followup after this one. The actual problem is that default template parameters for function templates are not supported by Visual Studios 2012 as indicated by this list. Since default template parameters are not supported by…
Didii
  • 1,194
  • 12
  • 36
0
votes
2 answers

Partial template specialization by overloading

I created a simple round template function with an extra template argument that defines the type the rounded value needs to be casted to before returning. template T round(U val) { T result; if (val >= 0) …
0
votes
1 answer

Is it legal to overload a function template with another function template?

I'm trying to create an overloaded method, where both are templated. One takes 4 arguments, and one takes 5. However I get an error along the lines of Error C2780 ... OutOfPlaceReturn ... : expects 4 arguments - 5 provided. ... A bunch of…
xaviersjs
  • 1,579
  • 1
  • 15
  • 25
0
votes
2 answers

Function Template Specialization using Base Class

I've looked around and not found quite what I'm looking for. Basically I want a function template specialized by a base class. I don't know how to make it use the specialized function. Meaning... class IBase { public: virtual std::string func()…
kmdreko
  • 42,554
  • 6
  • 57
  • 106
0
votes
0 answers

best-practice for #ifdef around VS2012 versus VS2013 for c++11 support?

What's a good/best way to toggle some function declarations based on VS version? context: I need to build on linux and windows and keep hitting cases where GCC compiles fine but VS2012 lacks some c++11 features. I'd like to #ifdef out the…
0
votes
2 answers

Meaning of Syntax within method-template

i have two questions about the following code: 211 template 212 tmp > 213 limitedSurfaceInterpolationScheme::flux //Return the interpolation …
Streight
  • 811
  • 4
  • 15
  • 28
0
votes
2 answers

Specializing member function template of a non-template class

Is the following specialization of the member function template bar valid? It compiles on gcc 4.5.3 and VS .NET 2008. I'm confused because I vaguely recall reading that function templates cannot be specialized. struct Foo { template
Olumide
  • 5,397
  • 10
  • 55
  • 104
0
votes
1 answer

Function template specialization not called for derived type

I have a function template that I have specialized for a specific type. I'm having trouble getting the specialized version to be called in certain circumstances. To illustrate struct Base {}; struct Derived : public Base {}; template
1 2 3
26
27