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
4
votes
1 answer
DLL + export class + template member func = unresolved external symbol. Any chance to fix?
First of all, This is not a duplicate question, because 1) this is a linker problem, compiler is passed successfully because I have explicitly instantiated. 2) It's not about template class, but template member function, 3) I have some restrictions…

user5280911
- 723
- 1
- 8
- 21
4
votes
3 answers
How does dereference operator (*) work when overloaded as a member function of a class?
When I looked up books and stack overflow article on operator overloading, I found the following:
When an overloaded operator is a member function, this is bound to the
left-hand operand. Member operator functions have one less (explicit)
…

sushrut619
- 843
- 8
- 20
4
votes
2 answers
Pimpl idiom implementation depending on a template function
Please consider the code below.
The template parameter is a handler class that must provide the function bar().
I'm using the Pimpl idiom to hide the implementation details of Foo. Prior to having a template parameter the constructor definition was…

ksl
- 4,519
- 11
- 65
- 106
4
votes
2 answers
What must be done for a static template function defined in a nested class to be declared a friend in a sibling nested class?
Using GCC 4.8.2 on Linux, I want to grant the factory method Create() access to the private constructor of class C, but I get "error: 'Create' was not declared in this scope" when attempting to declare a specialized friend. How do I get this to work…

Coder
- 441
- 2
- 17
4
votes
3 answers
Ambiguous call: int to double or bool
I have a Parameter class and I've overloaded the constructor to accept bools or doubles. When you give it an int, it fails to build:
error C2668: 'Parameter::Parameter' : ambiguous call to overloaded
function could be
…

colej1390
- 157
- 4
- 11
4
votes
3 answers
C++ circular dependency issue with function templates
I have several classes in a project I'm working on; the first is a Solver class, originally with a function template whose full definition is in the Solver header file, like so (just showing the bare necessities):
solver.h
class Solver {
public:
…

user2548415
- 51
- 4
4
votes
2 answers
Meaning of the following template function?
#define Create_Function(Type) \
template void Function( std::vector>&)
Create_Function(std::string);
I have seen the above code in legacy code but have no idea what the meaning of it. It is neither a regular…

q0987
- 34,938
- 69
- 242
- 387
4
votes
2 answers
Why doesn't my function skip trying to resolve to the incompatible template function, and default to resolving to the regular function?
std::string nonSpecStr = "non specialized func";
std::string const nonTemplateStr = "non template func";
class Base {};
class Derived : public Base {};
template std::string func(T * i_obj)
{
( * i_obj) += 1;
return nonSpecStr;…

Chris Morris
- 4,335
- 4
- 24
- 28
4
votes
4 answers
Why use template specializations?
I wonder why template specializations make sense?
Aren't the following things equivalent?
Template specialization:
template
void f(T t) {
something(t);
}
template <>
void f(int t) {
somethingelse(t);
}
Non-template function…

Sarien
- 6,647
- 6
- 35
- 55
3
votes
1 answer
Template function in eMbedded Visual C++ 4.0 for Windows CE
Does eMbedded Visual C++ 4.0 (SP4) support template functions? I get an error when I try to compile code that works fine in Visual C++ 6.0.
Here is my template function, which does compile:
template
NodeType* MyFunction()
{
//…

Petrus Theron
- 27,855
- 36
- 153
- 287
3
votes
1 answer
How to select the correct function overload?
What is the correct way to select the right function overload in the following case?
#include
#include
/** the correct overload **/
bool predicate( const char& c )
{
return c == '0';
}
/** the wrong overload…

nyarlathotep108
- 5,275
- 2
- 26
- 64
3
votes
2 answers
Forwarding references and template templates
Consider these two template functions:
template
void foo(T&& bar) {
// do stuff with bar, which may or may not be an instance of a templated class
}
template class T>
void foo(T&& bar) {
// do…

JAB
- 20,783
- 6
- 71
- 80
3
votes
0 answers
c++ Unable to resolve overloaded function template
First, I don't understand why the compiler can't seem to resolve the offending code (wrapped by the ENABLE_OFFENDER macro). The two get() methods have very different call signatures. So, perhaps a c++ language lawyer could help explain why I'm…

Boreet
- 53
- 1
- 4
3
votes
2 answers
Calling a zero argument template function pointer with variadic template argument?
Here is a code snippet from a post at Functional C++ blog, describing how a generalized function evaluation can be implemented.
My question is how can you declare template function pointer f like R(C::*f)() with no arguments and still be able to…

Benji Mizrahi
- 2,154
- 2
- 23
- 38
3
votes
1 answer
Tagged dispatch with variadic templates, c++11
I'm trying to make a function accept different arguments depending on the enum.
// cake.h
#pragma once
#include
enum class TYPE { CupCake, Jelly, BirthdayCake };
struct cake_tin { /* etc etc */ };
/** Fills in different ingredients for…

dpington
- 1,844
- 3
- 17
- 29