Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function template overloading clang++

g++ 4.8.1 and clang++ 3.4 give different results for next code:

// simplified code from a Logger module
#include <iostream>

template<class T> void tf(const T*) {  // clang++ 
    std::cout << "void tf(const T*)\n"; 
}

template<class T> void tf(T) {  // g++
    std::cout << "void tf(T)\n"; 
}

int main(){ 
    typedef std::ios_base& (*ph)(std::ios_base&);
    ph p = std::hex;
    tf(p); // or just tf(std::hex)
}

I can't figure out which variant is correct (C++ 03).

like image 534
gruzovator Avatar asked Oct 02 '13 06:10

gruzovator


People also ask

Can a function template be overloaded?

You may overload a function template either by a non-template function or by another function template. The function call f(1, 2) could match the argument types of both the template function and the non-template function.

What is overloading a function template?

Template Function Overloading:The name of the function templates are the same but called with different arguments is known as function template overloading. If the function template is with the ordinary template, the name of the function remains the same but the number of parameters differs.

What is C++ template name?

A template is a simple yet very powerful tool in C++. The simple idea is to pass data type as a parameter so that we don't need to write the same code for different data types. For example, a software company may need to sort() for different data types.


1 Answers

A pointer to function is not a pointer to an object and talking about const-ness of a pointer to function doesn't make sense in C++.

IMO g++ is right because hex qualifies as a pointer to function, but not as a const * to anything.

In the first case the template parameter is not a "pointer" but a "pointer to object".

There's no such a thing as a generic "pointer" in C++... you have pointers to function, pointers to object or pointer to members. Each of the three has different rules and is incompatible with the others.

Admittedly the null pointer brings in some confusion...

like image 175
6502 Avatar answered Sep 29 '22 14:09

6502