Question

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).

Was it helpful?

Solution 2

I've tried to submit this as a bug to llvm bugzill. The answer was:

This is a GCC bug. See:

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1584

OTHER TIPS

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top