函数使用const重载

时间:2017-03-31 20:57:40

标签: c++ overloading

struct A
{
    void fn(double a) const {}
    void fn(int a){}
};
int main()
{
    A().fn(1.);
}

对于上面提到的函数,为什么编译器会产生歧义;两种类型都不同。

1 个答案:

答案 0 :(得分:1)

为什么要将int仅传递给非常量A

每个成员函数有两个参数thisa。因此,const A*需要thisdouble需要a,非A*int需要A

并且调用与其他任何选项都不完全匹配,因为您有非const doubleA()。因此编译器可以将const A转换为double,或将int转换为#include <boost/spirit/home/x3.hpp> #include <iostream> namespace x3 = boost::spirit::x3; template <typename... Args> auto negate(Args&&... p) { return +(x3::char_ - x3::char_(std::forward<Args>(p)...)); }; int main() { std::string input("all the king's men and all the king's horses"), parsed; if (parse(input.begin(), input.end(), negate("horse"), parsed)) std::cout << "'" << input << "' -> '" << parsed << "'\n"; } 。它无法决定哪个是最好的。

相关问题