Functions that serve as a pattern for creating other similar functions. The basic idea behind template functions is to create a function without having to specify the exact type(s) of some or all of the variables.
Questions tagged [template-function]
121 questions
7
votes
1 answer
Why isn't automatic downcasting applied to template functions?
Someone asked this question about string appending. It's string s; s = s + 2; not compiling. People gave answers stating that operator+ is defined as a template function while operator+= is not, so auto downcasting (int(2) to char(2)) is not…

iBug
- 35,554
- 7
- 89
- 134
7
votes
2 answers
How to specialize template member function?
I have the following template method:
struct MyStruct
{
// ...
template
void readField(std::istream& in, T& data)
{
read(in, data);
data = ntohl(data);
}
};
template<>
void…

Patryk
- 22,602
- 44
- 128
- 244
7
votes
1 answer
Processing of uninstantiated template functions
The following code compiles in Visual C++ 2013 but not under G++ 4.8.2:
template
int MyFunc(T& t)
{
return static_cast(CCodes::blah);
}
template<>
int MyFunc(float& t)
{
return 0;
}
int main() {
float f = 10.f;
return…

Tom
- 7,269
- 1
- 42
- 69
7
votes
3 answers
Name conflict between template struct and template member function
In the following, GCC confuses template struct name with template member function name of class A, while Clang compiles fine (live example):
template
struct name {};
struct A
{
template
void name() { }
};
template

iavr
- 7,547
- 1
- 18
- 53
6
votes
2 answers
Template class type-specific functions
Ok, so i have this template class, which is kinda like one-way list.
template List
and it have this inside function print
public:
void Print();
which, as you can guess, prints the list contents from begining to end;
However, as…

Yury Elburikh
- 75
- 1
- 1
- 8
6
votes
2 answers
passing member-function as argument to function-template
Consider three ways to implement a routine in c++: through functors, member functions, and non-member functions. For example,
#include
#include
using std::cout;
using std::endl;
using std::string;
class FOO
{
public:
void…

vagoberto
- 2,372
- 20
- 30
6
votes
4 answers
template friend functions of template class
I have the following template class and template function which intends to access the class' private data member:
#include
template
class MyVar
{
int x;
};
template
void printVar(const MyVar& var)
{
…

timrau
- 22,578
- 4
- 51
- 64
6
votes
1 answer
Templates: Only execute method if class has it
I want to write a function which executes the method of some templated class, but should also compile fine if the class doesn't have it. In that case, it should just not call the function.
struct A
{
void func() {}
};
struct B
{
};
template…

TravisG
- 2,373
- 2
- 30
- 47
6
votes
2 answers
using and overloading a template member function of a base class?
In the following, struct Y overloads X's member function f. Both overloads are template functions, but take different arguments (typename and int), to be explicitly specified:
struct X
{
template static bool f() { return true;…

iavr
- 7,547
- 1
- 18
- 53
5
votes
1 answer
Specialization of template function after point of use will break the compilation
Consider next example :
#include
template< int a >
void foo();
int main(int argn, char* argv[])
{
foo<1>();
}
template<>
void foo<1>()
{
std::cout<<1<

BЈовић
- 62,405
- 41
- 173
- 273
5
votes
3 answers
Coupling Static Visitor with a Static Polymorphism Hierarchy
The purpose of my program is to create a list of data which i can visit with a set of static visitors while using static polymorphism in my class hierarchy.
I have created a hierarchy of classes utilizing static polymorphism through CRTP:
class…

Kognido
- 51
- 3
5
votes
3 answers
Function parameter type using decltype
Note: the example provided in this question is not production code and has no sense at all. It is just there to illustrate my problem.
I was testing the possibilities of decltype, especially if it is used to deduce function parameter types, and ran…

nshct
- 1,197
- 9
- 29
5
votes
1 answer
Type deduction of const references in templated functions
Below I have a template function named ProxyCall, which accepts an object, a member function and its arguments. It simply forwards the call to the member function.
I would like to be able to call the function without using template qualifiers…

Alf
- 752
- 1
- 7
- 13
5
votes
4 answers
adapting a non-constexpr integral value to a non-type template parameter, and code bloat
Consider a function object F taking a constexpr size_t argument I
struct F
{
template
constexpr size_t operator()(size ) const { return I; }
};
wrapped within a type size , where (for brevity)
template
using size…

iavr
- 7,547
- 1
- 18
- 53
5
votes
1 answer
Template function specialization vs. overloading
From some slides about template specialization:
#include
using namespace std;
template
X& min(X& a, X& b)
{
return a > b ? b : a;
}
int& min(int& a, int & b)
{
// rewrite of the function in the case of int:
cout…

Idan
- 5,365
- 5
- 24
- 28