function template overloading clang++

≡放荡痞女 提交于 2019-12-08 14:59:25

问题


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


回答1:


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




回答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



来源:https://stackoverflow.com/questions/19131309/function-template-overloading-clang

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!