我一直在尝试使用C++17 (以及更新版本)中的模板参数推导,并尝试编译来自cppreference.com的这个示例:
#include <functional>
int func(double) { return 0; }
int main() {
std::function f{func}; // guide #1 deduces function<int(double)>
int i = 5;
std::function g = [&](double) { return i; }; // guide #2 deduces function<int(double)>
}
在他们网页上的编译器中编译和运行正常,但是当我尝试在我的Macbook Pro上编译时失败了,显示错误:
error: no viable constructor or deduction guide for deduction of template arguments of 'function'
我已经在设置为C++17的Xcode(v10.1)项目中尝试过,并直接使用-std=c++17运行clang。
我还在不同的在线编译器上编译了上面的示例,例如这里:https://godbolt.org/z/ERliha
我也验证了std :: pair的类型推导是否有效,所以我不能确定:
- 当我搞交叉编译器时,是否弄错了我的工具链。
- Apple的工具链中是否缺少此类的推导向导。
- 我是否运行着旧版的工具链(我不知道如何检查)。
- ...(任何其他原因)
clang --version
命令来检查 Clang 的版本。 - chris