编译器使用错误的函数原型?

Dud*_*che 6 c++ stl const map

我遇到了一个我不理解的编译问题,我在下面对其进行了简化解释.

基本上,它涉及两个不同的getter(一个const和一个const),它们返回一个容器(在这个例子中是一个映射),它们分别是const,非const value_type.

令我困惑的是,在下面的示例中,编译器似乎无法在非const对象上使用const getter:

#include "stdafx.h"
#include <utility>
#include <map>

class TestObject
{
public:

    TestObject() {}
    virtual ~TestObject() {}
};

typedef std::pair<const TestObject*, const TestObject*> ConstTestObjectPair;
typedef std::pair<TestObject*, TestObject*> TestObjectPair;

class Test
{
    TestObject* m_pObject;

public:

    Test() {m_pObject = new TestObject();}
    virtual ~Test() {delete m_pObject;}

    std::map<unsigned, ConstTestObjectPair> GetObject() const
    {
        std::map<unsigned, ConstTestObjectPair> map;
        map.insert(std::make_pair(0, std::make_pair(m_pObject, m_pObject)));
        return map;
    }

    std::map<unsigned, TestObjectPair> GetObject()
    {
        std::map<unsigned, TestObjectPair> map;
        map.insert(std::make_pair(0, std::make_pair(m_pObject, m_pObject)));
        return map;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    Test* pTest = new Test();
    const Test* pConstTest = pTest;

    std::map<unsigned, ConstTestObjectPair> CTO = pTest->GetObject(); // Not compiling, I don't get why!!!
    CTO = pConstTest->GetObject();

    std::map<unsigned, TestObjectPair> TO = pTest->GetObject();
    //TO = pConstTest->GetObject(); // Not working, this is expected

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用VS2010和gcc,并且都不接受编译此代码.这是VS2010返回的编译错误:

1>c:\test.cpp(48): error C2440: 'initializing' : cannot convert from 'std::map<_Kty,_Ty>' to 'std::map<_Kty,_Ty>'
1>          with
1>          [
1>              _Kty=unsigned int,
1>              _Ty=TestObjectPair
1>          ]
1>          and
1>          [
1>              _Kty=unsigned int,
1>              _Ty=ConstTestObjectPair
1>          ]
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下为什么编译器无法在非const对象上找到/使用正确的原型吗?

非常感谢!

Tay*_*ter 5

如果您真的很好奇,请查看C++ 03标准的第13.3.3节,该标准描述了如何确定"最佳可行功能".以下是一些重点:

最佳函数的选择标准是参数的数量,参数与候选函数的参数类型的匹配程度,对象与隐含对象参数的匹配程度(对于非静态成员函数)以及对象的某些其他属性.候选功能.[注意:过载分辨率选择的功能不能保证适合上下文.其他限制(例如函数的可访问性)可能使其在调用上下文中的使用不正确.]

然后:

如果只有一个可行的函数比所有其他可行的函数更好的函数,则它是由重载决策选择的函数

请注意,此条件中未提及函数的返回类型.因此,非const方法被选为最有效的方法,因为它的"隐含对象参数"(本质上是"this"指针)是非const的.这一切都发生在检测到与返回类型的冲突之前.

要解决这个问题,我会:

  • 更改您的设计,以便ConstTestObjectPair不需要,您可以使用const TestObjectPair(首选解决方案)
  • 在需要时将非const对象转换为const对象