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

Compiler error `assigning incompatible type within if statement`

The compiler keeps assigning incompatible types during the build. error Message: error: assigning to 'int' from incompatible type 'QString' typeduserproperty.cpp:115:28: note: in instantiation of member function…
3
votes
2 answers

Interface and template functions

I'm trying to have interface over two different classes where implementation of a function is in the subclass. It works for regular functions, but unfortunately not for template functions. See example: import std.conv; import std.stdio; interface…
katafrakt
  • 2,438
  • 15
  • 19
3
votes
0 answers

Options to reduce the number of template instantiations for std::vector of trivial type

Suppose I have the following sample code just to instantiate the insert method of std::vector for at least two trivial types: #include void insert(std::vector& v, int const* a, int const* b) { v.insert(v.end(), a, b); } void…
3
votes
1 answer

Specializing a Template With a Return Type Derived by decltype in Visual Studio

Given the following code: template decltype(ValueType{} == ValueType{}) Compare(const ValueType& value, const ValueType& expected) { return value == expected; } template<> decltype(float{} == float{}) Compare(const…
3
votes
1 answer

template specialization taking a reference to const

I'm trying to understand how template specialziations work. I've got the following function template: template void function(const T &t1, const T &t2) { std::cout << "in function template: " << t1 << ", " << t2 << std::endl; } now,…
Luca
  • 1,658
  • 4
  • 20
  • 41
3
votes
1 answer

Is a fully specialized template function the same as a regular function?

If I have: template bool name (std::string); template <> bool name(std::string); What is the difference between the fully specialized function and my other regular functions. For example in the header I would have to have these…
Daniel Duque
  • 159
  • 9
3
votes
1 answer

Can Template Specializations go in my .cpp?

Lets say that I have this: struct foo { template void bar(const T param) { cout << param << endl; } }; Now I want to add the specialization: template <> void bar(const char param) { cout << static_cast(param) <<…
3
votes
1 answer

Can a shared pointer call a function template argument

This post answered my original question: how can I can I template on a member function? The answer has the code: struct Foo { void Bar() { // do something } }; template void Call(TOwner *p) { …
ad_ad
  • 375
  • 3
  • 14
3
votes
2 answers

Creating my own custom JQuery templating engine?

I had a quick search in stackoverflow.. but couldn't find anything quite what I was after. I am trying to understand/get some pointers on how to build my own VERY simple templating engine for jQuery. I basically have a standard AJAX call to get some…
Dav.id
  • 2,757
  • 3
  • 45
  • 57
3
votes
1 answer

C++ friend function template overloading and SFINAE different behaviors in clang++, g++, vc++ (C++14 mode)

So, the following code builds and runs successfully under clang++ (3.8.0), but fails both under g++ (6.3.0) and vc++ (19.10.24903.0). Both g++ and vc++ complain about redefinition of operator&&. Does anyone know which compiler is at fault here. For…
3
votes
2 answers

Function templates in python

I would like to know how would I use similar code to template < typename T> in python, as it is used in the C++ code example: template unsigned int counting_bit(unsigned int v){ v = v - ((v >> 1) & (T)~(T)0/3); …
Muhammad Ali Qadri
  • 606
  • 2
  • 8
  • 21
3
votes
1 answer

explicit specialization for function template c++

template bool validate(const T& minimum, const T& maximum, const T& testValue) { return testValue >= minimum && testValue <= maximum; } template <> bool validate( const char& minimum, const char& maximum, …
Andy
  • 127
  • 2
  • 9
3
votes
1 answer

Can't see function-template overload from recursive call

The question is, why it doesn't work if tuple is not at the first position of its parent. Looks like it doesn't see overload for tuple from inside _after_print. _print(make_tuple(), 0); Evaluates to this: a tuple not a tuple And _print(0,…
Yola
  • 18,496
  • 11
  • 65
  • 106
3
votes
3 answers

Is there a C++ equivalent to Java's Collection interface for STL container classes?

I would like to pass arbitrary container as an argument of function and iterate over it (no erasing nor pushing elements). Unfortunately it looks like there is no standard way of doing this. First solution which comes to my mind is an interface…
mip
  • 8,355
  • 6
  • 53
  • 72
3
votes
1 answer

Function overloading with template

I have the following code. #include using namespace std; void print(int& number){ cout<<"\nIn Lvalue\n"; } void print(int&& number){ cout<<"\nIn Rvalue\n"; } int main(int argc,char** argv){ int n=10; print(n); …
01000001
  • 833
  • 3
  • 8
  • 21