此程序在使用 MSVC 编译时打印1 1
而不是打印1 2
(直到 VS 2015)。
f1.cpp:
#include <functional>
static std::function<int ()> helper() {
struct F { int operator()() { return 1; } };
return F();
}
std::function<int ()> f1() { return helper(); }
f2.cpp:
#include <functional>
static std::function<int ()> helper() {
struct F { int operator()() { return 2; } };
return F();
}
std::function<int ()> f2() { return helper(); }
主.cpp:
#include <functional>
#include <iostream>
std::function<int ()> f1();
std::function<int ()> f2();
int main() {
std::cout << f1()() << " " << f2()() << "\n";
}
就好像 的不同定义F
正在破坏 ODR。但是本地类不应该是不同的吗?有趣的是,如果我们F
用 lambda 函数替换,则不会发生冲突。
那么这是一个编译器错误还是我误解了一个定义规则?