【发布时间】:2023-04-08 04:59:01
【问题描述】:
以下代码可以编译,但在 VC++ 2015(发行版)中会产生未定义的输出,并会出现 other compilers 的运行时错误。
#include <functional>
#include <iostream>
int main()
{
std::function<int(int)> f = [](int x) { return x; };
std::function<const int&(const int& x)> g = f;
std::cout << g( 42 ) << std::endl;
}
为什么允许分配g = f;?
【问题讨论】:
-
用apple clang完美编译执行
-
高度相关/可能的欺骗:stackoverflow.com/q/32871606/2069064