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

How to implement a universal function for both sequence and associative container?

I am thinking to write a function that works both for sequence and associative container. That's something like template bool has_val(const C& c, const V& v)` Inside the function, I would like to Check if…
4
votes
1 answer

How can I use different struct as template argument in a template function?

I am writing a template function like that: template std::string EncodeData(int DataType, T Data, std::string ReadCommandID, std::string& ThisID); The type T that I want to use is some different structs, for example: struct A { int…
4
votes
1 answer

'Candidate template ignored: couldn't infer template argument' with std::set

I'm writing a function to manipulate a set with it's iterators, and it's giving me the error candidate template ignored: couldn't infer template argument The minimum reproducible code is as follows: #include using namespace…
4
votes
6 answers

Template argument as function unnamed argument

This question continues Non-static data members class deduction Here are the unnamed argument functions, that I'm using to return std::string representation of the data type struct Boo {}; struct Foo {}; std::string class2str(const double) {…
4
votes
1 answer

C++ Variadic functions with no argument

I have multiple classes (Foo and Bar here for simplicity) struct Bar {}; struct Foo {}; and a function that takes a single template parameter and does something based on that type: template constexpr void doSomething() { cout << "Am I…
4
votes
1 answer

Why is my function template specialization rejected by VS2017 and not by VS2015?

I have a trait class that associates types to integer values. struct traits { private: template struct type_impl {}; template<> struct type_impl<1> { using type = int; }; // ... public: template using type =…
4
votes
1 answer

Can C++ free functions be aliased?

I have a namespace with one highly templated free function, such as: namespace a { template void f(T t, K k, std::vector h_vec = {}) { /* body */ } } Inside another namespace, for convenience I…
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
4
votes
2 answers

C++ static template class member as friend template function default parameter

Why does using static template class member as friend template function default parameter give me compile error in c++? How to slove? Here is the code: #include template void func(T n); template class…
4
votes
1 answer

template overloading results in linker error / strange behaviour

with the following minimal example i get a linker error on my local system in visual studio 15.8.7 (standard console app with standard settings (just removed precompiled headers)): "Error LNK1179 invalid or corrupt file: duplicate COMDAT…
phön
  • 1,215
  • 8
  • 20
4
votes
3 answers

C++ Template type deduction for temporary value

#include #include using namespace std; template void wrapper(T& u) { g(u); } class A {}; void g(const A& a) {} int main() { const A ca; wrapper(ca); wrapper(A()); // Error } Hi I have a question…
Jaebum
  • 1,397
  • 1
  • 13
  • 33
4
votes
3 answers

Where to put a member function template

An aspect of C++ that periodically frustrates me is deciding where templates fit between header files (traditionally describing the interface) and implemention (.cpp) files. Templates often need to go in the header, exposing the implementation and…
beldaz
  • 4,299
  • 3
  • 43
  • 63
4
votes
2 answers

Parameterization and "function template partial specialization is not allowed"

This is a continuation of What is the function parameter equivalent of constexpr? In the original question, we are trying to speed-up some code that performs shifts and rotates under Clang and VC++. Clang and VC++ does not optimize the code well…
jww
  • 97,681
  • 90
  • 411
  • 885
4
votes
4 answers

Logical error in Function template

My professor has given me this assignment. Implement a generic function called Max, which takes 3 arguments of generic type and returns maximum out of these 3. Implement a specialized function for char* types. Here's my code : #include…
Searock
  • 6,278
  • 11
  • 62
  • 98
4
votes
1 answer

What is the difference between using std::enable_if as function argument vs template argument?

I wonder what is the difference between using std::enable_if as function argument vs template argument? I have the following 2 function templates: #include template void f_function(T, typename…
Patryk
  • 22,602
  • 44
  • 128
  • 244
4
votes
1 answer

Define friend function template of class template

I want to define a function template of a class template. The code looks like this. template struct test{ private: int value; template friend auto foo(test const t){ test r; r.value = t.value; …