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

Return a class template with value template arguments from function

Suppose I have a simple template class: template class ConsecutiveMatcher { public: bool operator () (ElementType lhs, ElementType rhs) { return lhs == Element && rhs == Element; …
Felix Dombek
  • 13,664
  • 17
  • 79
  • 131
0
votes
2 answers

Class Template + Function Template

when I tried to create a template class as follows: template class Variant { public : std::string toString(); // var.toString() template std::string toString(); // var.toStrint(); protected: …
0
votes
2 answers

compile error in template member conversion operator

I'm trying to write a conversion operator function template in a class and running into some compile errors which I don't fully understand. class ABC { }; class BBC:public ABC { }; template class TestPtr { public: TestPtr(T*…
hawk
  • 1,827
  • 2
  • 14
  • 28
-1
votes
1 answer

a type template function that accepts only a few type

fn foo() -> f32 { return 0.0; } So I want to define a function that accepts only certain types (e.g. i32, i64, f32) as template parameter. For this case I hope to define only foo(), foo(), foo() and nothing more. How do I…
Rahn
  • 4,787
  • 4
  • 31
  • 57
-1
votes
1 answer

most succinct or canonical way to bind class members for purposes of input, output, etc. using power of modern C++

My application requires end-users to write some classes to which I'll feed input data and take output data from. These user classes can be subclasses of a base class I provide. A way that's worked since the 90s would be a template allowing members…
-1
votes
2 answers

How to achieve polymorphism with templated functions?

In my project, I have a base abstract class with an interface, which derived classes implement. These derived classes have generic functions that accept parameters of different types. I have written these generic functions in my derived classes…
KulaGGin
  • 943
  • 2
  • 12
  • 27
-1
votes
3 answers

Should lambdas replace function templates in C++?

In C++ I can write: auto sum(const auto& x1, const auto& x2) { return x1 + x2; } int main() { cout << sum(1, 2) << endl; cout << sum(1, 2.3) << endl; cout << sum(1.4, 2.3) << endl; } Which outputs: 3 3.3 3.7 To write same code with…
Andrey Rubliov
  • 1,359
  • 2
  • 17
  • 24
-1
votes
1 answer

Inconsistent error using template functions

(This question has bounty for anyone willing to take a shot) Hi I have defined overloading template function with container class as arguments (Here CntrlCls1 = RWTValOrderedVector and CntrlCls2 = RWTPtrSortedVector) template void…
Gaurav
  • 794
  • 2
  • 11
  • 32
-1
votes
1 answer

Working with std::generator_canonical in a templated static method within a class interface

I am working on a class that does not instantiate an instance; it has a protected constructor and all methods are static. This class I have simplifies the calls to various random engines found in I have a similar class that does the same…
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
-1
votes
1 answer

Problems writing a function template for a map of object pointers

I have a function template to print a map of string with object pointers: //template function to print map of pointers template void printMap(std::map&_map) { //test print line cout << "Test function template for map…
user3956566
-2
votes
1 answer

Using function templates for function overloading

I'm trying to overload a function while adhering to the DRY principle. The only difference between overloads are argument types, so I chose to use templating. I came up with essentially the following code: a.h: #ifndef A_H #define A_H #include…
-2
votes
2 answers

error: size in array new must have integral type [-fpermissive]

I'm using dynamic memory allocation to create new objects and the following error keeps showing up when I try to compile. I have dimensions_ created as an unsigned int so I'm not sure why this error shows up. EuclideanVector.h:69:40: error: size in…
1 2 3
26
27