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

C++: template function with explicitly specified reference type as type parameter

I was playing with C++ template type deduction and managed to compile this little program. template void f(const T& val) { val = 1; } int main() { int i = 0; f(i); } It compiles on all major compilers but I do…
4
votes
1 answer

Separate declaration and definition of specialization of template function : different behaviour for member and nonmember functions

I want to declare specialization for function template, but define it later in source file. Consider next example: .hpp // approach #1 template const char *GetTypeName(); template <> const char *GetTypeName(); // approach…
4
votes
1 answer

Generic solution to get simple values from lua file into c++

I'm trying to use a Lua file as a config or an ini. I have succeeded but my solution irritates me. Specifically, get_double, get_int and get_string functions needs to be done in a reusable way. I ran into problems creating function templates that…
kirill_igum
  • 3,953
  • 5
  • 47
  • 73
4
votes
2 answers

C++: Function Template to Handle Ints and Strings

I'm working on a programming assignment, to make a function template that can handle ints and doubles. I've done that, but for fun, I wanted to make it able to handle strings, as well. Here is the function below. How would I go about making it…
Adam_G
  • 7,337
  • 20
  • 86
  • 148
3
votes
1 answer

Calling static function template within dependent scope

Suppose I have a static function template template void ft() within a struct template template S, and I want to call ft from another function template template void g(), passing the bool template parameter from g to…
Ose
  • 726
  • 7
  • 13
3
votes
3 answers

specification of function templates

I would like to create a function template where the class T is limited to only derived classes of a special base class T_base. What is the efficient way to accomplish this? Thanks for your help!
3
votes
1 answer

call template function for each template parameter in constexpr std::array

Given a function print(void) and a constexpr std::array q={1,2,3}, I want a loop that calls print for each qi in q. My minimal example looks like this: #include #include template void print(){…
3
votes
1 answer

Non type template parameter in msvc does not compile

I studied about non-type template parameter and came to know that they must be compile time constant. Then I created the below given program that compiles with gcc as well as clang but fails to compile with msvc. I am working with c++20 and want to…
3
votes
2 answers

How can I minimize repeated template type names?

This snippet is a small example from a c++20 code base. It is a free function to multiply matrices with each other. The Matrix itself is templatized on ROWS and COLUMNS which, like the std::array, makes it a bit painful to use in function…
ulfben
  • 151
  • 8
3
votes
1 answer

Can I make separate definitions of function template members of a class template?

Here's a minimal code example to show what I'm attempting that works, but not how I'd like it to: #include #include #include struct string_tag { using R=const std::string; }; struct int_tag { using R=const…
3
votes
3 answers

Is there a clean way to forward template parameters to a templated function?

The following works #include template double func1(B x, B y) { A a = 3.0f; return a + x + y; } template double func2() { B x = 5.0; B y = 8.0; return func1(x,…
3
votes
1 answer

What is the variadic function template overloading precedence rule?

I'm using variadic function templates in the common recursive format and I need to change the behaviour of the function whenever I'm handling a vector. If the functions templates were not variadic, overloading works well, but with variadic function…
3
votes
3 answers

Defaulted Template in Template Function Requires Empty Angle Brackets <>

gcc 11.2 can't seem to compile this: template struct Test {}; template void foo(T& bar) {} int main() { Test t; foo(t); } but has no problem with template struct Test {}; template…
3
votes
1 answer

Deduct template parameter fail while using if constexpr

I am trying to figure out how the sfinae concept works in C++. But I can't convince the object-type compiler if bool is true or false. #include class A { public: void foo() { std::cout << "a\n"; } }; class B { public: void ioo()…
Ionut Alexandru
  • 680
  • 5
  • 17
3
votes
2 answers

How to add a parameter value when forwarding parameters to a variadic template function?

Suppose that I have two functions below, in the Foo() function, how can I pack the hw string into args and forward them to Bar()? I tried std::bind but that didn't work. template void Bar(Args&&... args) { // do…