Questions tagged [function-templates-overloading]
32 questions
14
votes
0 answers
Why does enable_if on the function parameter type influence overload resolution?
See code on godbolt
#include
#include
#include
template
void f(T, T) // 1
{
std::cout << "Primary\n";
}
template
void f(T, std::enable_if_t, T>) // 2
{
…

user119879
- 331
- 1
- 7
6
votes
1 answer
understanding std::is_base_of possible implementation from cppreference
Here we have is_base_of template implementation taken from cppreference.com:
namespace details {
template
std::true_type test_pre_ptr_convertible(const B*); //1
template
std::false_type…

mcz
- 63
- 2
- 3
6
votes
1 answer
Constrained function template vs un-constrained function template with different parameter type
In the following two template functions, one tries to be more constrained:
template
concept SmallVar = (sizeof(T) <= sizeof(int));
void print(SmallVar auto t) { // 1
std::cout << t << std::endl;
}
void print(const auto& t) { // 2…

Amir Kirsh
- 12,564
- 41
- 74
6
votes
1 answer
C++ primer 5th ed. function template overloading
In the book C++ Primer, there is an example about function template overloading:
// print any type we don't otherwise handle
template string debug_rep(const T &t)
{
cout << "debug_rep(T const&)\n";
ostringstream ret; // see §…

Maestro
- 2,512
- 9
- 24
5
votes
3 answers
Template deduction depends on another template deduction
Since std::format isn't supported everywhere, and I didn't want another large dependency like fmt, I wanted to quickly roll my own to_string solution for a number of types. The following is the code.
#include
#include
#include…

Jaan
- 330
- 2
- 9
5
votes
1 answer
What makes the overload fail between these two function templates?
Below is the pretty short example.
#include
template
struct A {};
template
void f(A>) {}
template
void f(A,…

Enlico
- 23,259
- 6
- 48
- 102
4
votes
1 answer
Usage of decltype in return type of function template removes error due to exception specification
I saw an answer to a question here. There the author of the answer made use of the fact that
exception specifications do not participate1 in template argument deduction.
In the answer linked above it is explained why the following doesn't…

Jason
- 36,170
- 5
- 26
- 60
4
votes
1 answer
Non-type template function overloading problem
Consider:
template
class SizeFlag {};
template
void asd(SizeFlag, SizeFlag) {
}
template
void asd(SizeFlag , SizeFlag) {
}
template
class…
3
votes
3 answers
Raw int pointer vs vector::iterator
I was trying to understand difference between a raw pointer and an vector iterator. However, the following program trips me out. Does template function have priority over non-template function?
Expected: hello! world!
Actual: hello! hello!
#include…

Hee Hwang
- 121
- 7
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…

Ivo
- 33
- 4
3
votes
1 answer
C++ function template overload: empty braces vs explicit int
There is some very basic example of C++ function template overloading resolution below (minimized from actual code)
struct S {
S() {}
S(int) {}
};
template
void foo(T x) { std::cout << "S" << std::endl; }
template <>
void…

Konstantin Vladimirov
- 6,791
- 1
- 27
- 36
1
vote
2 answers
Function Template Overloading with Different Return Types
Following code snippets are from Function template overloading.
How is it possible to overload functions/function templates with return type A vs A?
Did the page really mean that overload #1 & overload #2 compose valid function overload…

cpp
- 265
- 1
- 6
1
vote
1 answer
(non)ambiguous static overload within templated class
My class template NodeMaker has 3 static member function templates called create_node which are distinguished by their argument(s) using C++20 concepts. When calling NodeMaker<>::create_node(x) from main() everything works as I intended, but when…

igel
- 358
- 1
- 8
1
vote
1 answer
Why compiler(g++) cannot find the first add function?
template
void add(SBTNode*& t, const SBTNode* x)
{
if (t==SBTNode::getPNIL()) { t = x; return;}
if (x->keykey) add(t->left,x);
else if (t->keykey) add(t->right,x);
…

葛盈泽
- 81
- 4
1
vote
0 answers
C++ Primer 5th ed: function template overloading vs specialization
I have this example from C++ primer 5th ed:
template
int compare(T const& x, T const& y) // in the book const T&
{
cout << "primary template\n"; // I've added print statements
if(std::less()(x, y))
return -1;
…

Maestro
- 2,512
- 9
- 24