【发布时间】:2015-04-16 04:24:39
【问题描述】:
当我用 libc++ 运行这段代码时:
struct foo
{
foo(int x) : x(x)
{}
int x;
};
int main()
{
const auto select_x = std::mem_fn(&foo::x);
foo f(1);
printf("%i\n", select_x(f));
}
我收到这样的错误:
mem_fn.cpp:16:20: error: no matching function for call to object of type
'const std::__1::__mem_fn<int foo::*>'
printf("%i\n", select_x(f));
^~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/functional:1224:11: note:
candidate function not viable: 'this' argument has type 'const
std::__1::__mem_fn<int foo::*>', but method is not marked const
operator() (_ArgTypes&&... __args)
^
似乎 libc++ 缺少const 重载。它使用 libstdc++ 工作。这是 libc++ 中的错误吗?或者标准是否需要const 重载?
【问题讨论】: