c++ - 为什么两个函数有相同的地址?

标签 c++ function templates memory-address function-templates

考虑这个函数模板:

template<typename T>
unsigned long f(void *) { return 0;}

现在,我打印 f<A> 的地址和 f<B>作为:

std::cout << (void*)f<A> << std::endl;
std::cout << (void*)f<B> << std::endl;

如果在 MSVS10 中编译,为什么它们会打印相同的地址?它们不是两个不同的功能,因此应该打印不同的地址吗?

更新:

我意识到在 ideone 上,它打印出不同的地址。 MSVS10 优化代码,因为函数不依赖于 T以任何方式,所以它产生相同的功能。 @Mark 对此的回答和评论很有值(value)。 :-)

最佳答案

您需要转换为 void * :

std::cout << (void*)(ftype*)f<A> << std::endl;
std::cout << (void*)(ftype*)f<B> << std::endl;

如果转换为函数指针(或其他几个类的非空指针),它将被解释为 bool通过 operator<<对于 std::ostream (因此是 1 )。

关于c++ - 为什么两个函数有相同的地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9323273/

相关文章:

c++ - 错误 C2678 : binary '!=' : no operator found which takes a left-hand operand

templates - Golang 模板.ParseFiles "not a directory"错误

c++ - 模板和嵌套类型查找

c++ - 隐藏结构如何工作?

C++:如果在特征类或默认值中可用,则使用内部 typedef

javascript - 传递刚刚创建的参数

具有多个变量输入的 R 自定义 data.table 函数

c++ - 连接c++服务器时,flash socket发送错误数据

c++ - 在 std 算法中使用 std::move_iterator 并以 lambda 作为 pred 是否安全

function - Julia 中的卡方表函数