4

I am developing a cross platform code base where the initial work is done using MS VC2010 compiler.Later I compile it on Linux with GCC (4.7).In many cases I am receiving :

"No matching function for call .." error in GCC.I noticed that it complains mostly when method params are non constant references.For example this:

 void MyClass::DoSomeWork(ObjectSP &sprt, const std::string someName, const std::string anotherName, const std::string path, int index) {


        sprt->GetProp()->Update(path, false);

}

Once I change the method to this:

 void MyClass::DoSomeWork(const ObjectSP& sprt, const std::string& someName, const std::string& anotherName, const std::string& path, int index) {


        sprt->GetProp()->Update(path, false);

}

GCC stops complaining. Why does it happen and why does it not happen in VC compilers?

4

1 回答 1

9

将非常量引用绑定到临时对象是非法的。然而,从历史上看,VS 编译器对此没有那么严格。

因此,如果你有一个带有非常量引用的函数,并且你用一个临时对象(例如函数的返回值)调用它,g++ 会兼容,但 VS 不会。在这种情况下,g++ 是正确的。

如果可以的话,总是更喜欢 const 引用。

于 2013-09-30T09:41:56.230 回答