c++ - 模板返回类型为 : compiler fails at derived class with pointertype as template argument (MSVC 2013) 的虚基函数

标签 c++ templates pointers derived covariant

如果我使用模板参数 T=int* 从 CBaseInterface(参见下面的代码)派生,编译器将失败并出现错误 C2555。用于 T 的所有指针类型都会发生这种情况。如果我改用 typedef,则相同的代码可以正常工作。

// If _FALIS is defined, the compiler fails, else it succeeds
// (MS Visual Studio 2013 Update 2, 32 and 64 Bit native C++, Debug build).
#define _FALIS

#ifdef _FALIS
    #define PINT int*   
#else
    typedef int* PINT;
#endif

template <class T>
class CBaseInterface
{
public:
    virtual ~CBaseInterface() {}
    virtual const T Calculate() const = 0;
};

class CCalculator : public CBaseInterface<PINT>
{
public:
    CCalculator() {}
    virtual ~CCalculator() {}

    // error C2555: 'CCalculator::Calculate': 
    // overriding virtual function return type differs and is not 'covariant'
    // from 'CBaseInterface<int *>::Calculate'
    virtual inline const PINT Calculate() const final
    {
       return (PINT)&m_Item;
    }

protected:
    int m_Item = 0;
};

指针类型的问题在哪里?我很困惑,我在 Microsoft 的文档中找不到适合这种情况的任何内容。

希望你能帮助我。

最佳答案

区别在于派生类中const PINT的含义。

如果 PINTint * 的类型定义,则 const PINTint * const(常量pointer to mutable int) - 这很好,这就是定义基类函数返回的内容。如果你使用你的宏,那么你就有字面上的 const int *(指向常量 int 的可变指针),这是一种完全不同的类型。 typedef 被逻辑地替换到类型系统中,宏被盲目地替换为标记。

解决这个问题的一种方法是编写 PINT constconst (PINT)(因此 const 的绑定(bind)是显式的)。

而且你真的不应该使用宏。

关于c++ - 模板返回类型为 : compiler fails at derived class with pointertype as template argument (MSVC 2013) 的虚基函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23978577/

相关文章:

c++ - 检测类型是否为函数

c++ - 是否可以在 C++ 中查找类的表?

c - C中指针的声明

c - 结构体元素的初始化

c++ - 生成 Catmull Rom 样条并返回垃圾

c++ - 而大于负数则不起作用

包含 ddd 调试器选项的 C++ Makefile

c++ - 在不知道参数的情况下传递模板

c++ - 如何用sfinae检查,type是否有operator()?

c - 如何使用指针访问嵌套结构的成员?