例如,代码如下:
struct A { A(int); };
struct B { B(A); };
int main()
{
B b{{0}}; // OK
B c({0}); // error
}
Run Code Online (Sandbox Code Playgroud)
错误消息是:
f.cc: In function 'int main()':
f.cc:7:9: error: call of overloaded 'B(<brace-enclosed initializer list>)' is ambiguous
B c({0}); // error
^
f.cc:7:9: note: candidates are:
f.cc:2:12: note: B::B(A)
struct B { B(A); };
^
f.cc:2:8: note: constexpr B::B(const B&)
struct B { B(A); };
^
f.cc:2:8: note: constexpr B::B(B&&)
Run Code Online (Sandbox Code Playgroud) 为什么是std::less<int>()
一个函数对象
std::sort(vec.begin(),vec.end(),std::less<int>());
Run Code Online (Sandbox Code Playgroud)
但是std::less<int>
类型和操作符是函数调用,没有创建对象,或者我们可以引用的内存地址
我遇到了这段代码:
auto f3 = std::bind(&Foo::print_sum, &foo, 95, _1);
Run Code Online (Sandbox Code Playgroud)
在前面的代码中,我们可以将地址运算符应用于成员函数,而没有创建实例对象,这怎么可能?