Medium
Two handlers with the same body and a pointer comparison
Each of the three files below is its own translation unit, all built at -O2 with -ffunction-sections and linked with lld using -Wl,--icf=all. There is no LTO. What does the program print?
handlers.cpp
// handlers.cpp
int onRead(int fd) { return fd + 1; }
int onWrite(int fd) { return fd + 1; }
// names.cpp
using Handler = int (*)(int);
int onRead(int);
int onWrite(int);
const char* nameOf(Handler h) {
if (h == onRead) return "read";
if (h == onWrite) return "write";
return "unknown";
}
// main.cpp
#include <cstdio>
using Handler = int (*)(int);
int onWrite(int);
const char* nameOf(Handler h);
int main() {
std::puts(nameOf(onWrite));
}32 of 44 in Compilers