Questions tagged [template-function]

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.

121 questions
87
votes
4 answers

Why doesn't ADL find function templates?

What part of the C++ specification restricts argument dependent lookup from finding function templates in the set of associated namespaces? In other words, why does the last call in main below fail to compile? namespace ns { struct foo {}; …
Hugh
  • 8,872
  • 2
  • 37
  • 42
43
votes
4 answers

inline template function?

Do I need inline template functions if they are included in several cpp files? Thanks. template inline QString GetText(); template<> inline QString GetText() {return "true";} template<> inline QString GetText() {return "false";}
user1899020
  • 13,167
  • 21
  • 79
  • 154
20
votes
3 answers

Why doesn't C++11 implicitly convert lambdas to std::function objects?

I implemented a generic event emitter class which allows code to register callbacks, and emit events with arguments. I used Boost.Any type erasure to store the callbacks so they can have arbitrary parameter signatures. It all works, but for some…
gcv
  • 468
  • 4
  • 10
14
votes
1 answer

Comparison operator for std::vector fails to find comparison operator for T

The following very simple code won't compile #include #include namespace Foobar { struct Test { std::string f; std::uint16_t uuid; }; } bool operator==(const Foobar::Test& lhs, const Foobar::Test& rhs){ …
13
votes
6 answers

No Automatic Cast from `int` to `float` with Template Function

After years of coding in c++, today I was asked a simple question, but indeed I couldn't find its answer and so here I am guys. Besides wondering why this error is happening, I want to know how I can solve below error by modifying just the template…
Emadpres
  • 3,466
  • 2
  • 29
  • 44
12
votes
6 answers

C++: How to use type in template function to branch?

I am not quite proficient with templates. How do I write the a template function called get that chooses the array it gets from based on the template type? See the example below: struct Foo { int iArr[10]; char cArr[10]; // How to pick…
Ashwin Nanjappa
  • 76,204
  • 83
  • 211
  • 292
12
votes
3 answers

How to specialize std::begin?

I'm trying to specialize std::begin for a custom container. I'm doing this because I want to use range-based for with the container. This is what I have: class stackiterator { … }; class stack { … }; #include template <> stackiterator…
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
11
votes
1 answer

C++: candidate template ignored: invalid explicitly-specified argument for template parameter

I have this function header: template < bool src_alpha, int sbpp, int dbpp, typename T1, typename T2, Color (*getFunc)(T1 data, Uint8* addr), void (*putFunc)(T2 data, Uint8* addr, Color c) > static void OperateOnSurfaces(T1…
Albert
  • 65,406
  • 61
  • 242
  • 386
10
votes
1 answer

SFINAE : Delete a function with the same prototype

I wonder what is the difference between this code that works : #include #include template using is_ref = std::enable_if_t, bool>; template using is_not_ref =…
Antoine Morrier
  • 3,930
  • 16
  • 37
9
votes
1 answer

Why does std::is_same give a different result for the two types?

In the code below, why do the two ways of invoking fun: fun(num) and fun(num), give a different result when compiling? #include using namespace std; template
9
votes
3 answers

Why is std::swap not using swap idiom?

As proper use of std::swap is: using std::swap; swap(a,b); It is a bit verbose but it makes sure that if a,b have better swap defined it gets picked. So now my question is why std::swap is not implemented using this technique so user code would…
9
votes
4 answers

How to call destructor of type in template?

For example, we have a function like that: template void construct_and_destruct(TYPE & object) { //... } We cant call constructor and destructor like object.Type() and object.~Type() (no true now) ( Whyy? =C ) To call the…
4Bytes
  • 325
  • 3
  • 8
8
votes
2 answers

Inlining Template Specialization

If I have a header foo.h which I include all over my project, it seems to work fine when all it contains is: template void foo(const T param) { cout << param << endl; } But I get one definition rule (ODR) errors when I add a…
8
votes
2 answers

How to write a template function that takes an array and an int specifying array size

For a university exercise, I have been asked to write a template function "print();", which takes two arguments, 1: an array of a generic type, and 2: an int specifying the size of the array. The function should then print out every item in the…
willfo
  • 241
  • 1
  • 2
  • 12
7
votes
1 answer

Passing function template specializations to a variadic template function

I have no problem passing the address of a function template specialization to a regular template function: template void f(T) {} template void foo(A, B) {} int main() { foo(&f,…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
1
2 3
8 9