协变量返回类型 [英] covariant return type

查看:65
本文介绍了协变量返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ 10.3/5

"重写的返回类型功能应与覆盖的返回类型函数或与类协变功能.如果函数D :: f覆盖函数B :: f,返回函数类型是协变的如果他们满足以下条件准则:

"The return type of an overriding function shall be either identical to the return type of the overridden function or covariant with the classes of the functions. If a function D::f overrides a function B::f, the return types of the functions are covariant if they satisfy the following criteria:

-都是指向的指针类或对class98的引用)

— both are pointers to classes or references to classes98)

-B :: f的返回类型中的类与返回D :: f的类型,或者是明确易懂的直接或该类中的间接基类D :: f

— the class in the return type of B::f is the same class as the class in the return type of D::f, or is an unambiguous and accessible direct or indirect base class of the class in the return type of D::f

两个指针或引用都具有相同的简历资格D :: f的返回类型中的类类型具有与或相同的简历资格简历水平不及同等级键入B :: f的返回类型.

struct A{};
struct B : A{};

struct X{
    virtual const A * const f(){return 0;}
};

struct Y : X{
    virtual const B * volatile f(){return 0;}
};

int main(){
    Y b;
    X &r = b;
    r.f();
}

我编写了上面的实验代码,发现Comeau的错误/警告是不一致的.第9行中的警告似乎表明return类型的cv限定词是没有意义的.这就是导致代码格式错误的原因.

I wrote the above experimental code and find Comeau's error/warning to be inconsitent. The warning in Line 9 seems to indicate that cv qualifier in return type is meaningless. And it is this very reason which leads to the code being ill-formed.

"ComeauTest.c", line 5: warning: type qualifier on return type is meaningless
virtual const A * const f(){return 0;}
                  ^

"ComeauTest.c", line 9: warning: type qualifier on return type is meaningless
virtual const B * volatile f(){return 0;}
                  ^

"ComeauTest.c", line 9: error: return type is not identical to nor covariant with
      return type "const A *const" of overridden virtual function function
      "X::f"
virtual const B * volatile f(){return 0;}
                           ^

问题是,Comau在9号线发出警告"消息是否正确?我知道这是一种实现定义的行为,而Comeau只是试图变得更好.但是在这种情况下,这是最令人困惑的.

So the question is, is Comeau right in giving the "warning" message at Line 9? I know that is an implementation defined behavior and Comeau is just trying to be nice. But in this case, it is at the best confusing.

推荐答案

第5行和第9行上的警告是返回类型上的类型限定符无意义",这是因为非类类型的右值从来没有cv限定.

The warnings on lines 5 and 9 that the "type qualifier on return type is meaningless" are because non-class type rvalues are never cv-qualified.

由于按值返回的函数的结果是右值,并且指针是非类类型,因此即使返回类型表明它是cv,返回的指针也不是cv限定的.

Since the result of a function that returns by value is an rvalue and a pointer is a non-class type, the returned pointer is not cv-qualified, even if the return type says that it is.

此警告与协方差无关.以下功能将导致相同的警告:

This warning has nothing to do with covariance. The following function would cause the same warning:

int* volatile f() { return 0; }

至于10.3/5中引用的文本:

As for the cited text from 10.3/5:

指针或引用具有相同的cv资格

both pointers or references have the same cv-qualification

这是指返回类型的顶级限定(即, const int * volatile 中的 volatile ).尽管顶级资格毫无意义,但它确实会影响函数的类型,因此,鉴于上面的 f 声明,此代码段是不正确的:

This is referring to the top-level qualification of the return type (that is, the volatile in const int* volatile). While the top-level qualification is meaningless, it does affect the type of the function, so given the declaration of f above, this snippet is incorrect:

int* (*q)() = f; // error:  can't convert int* volatile (*)() to int* (*)()

同样,如果派生类成员函数中返回类型的顶级cv限定与基类中返回类型的顶级cv限定不匹配,则派生类成员函数将不被覆盖基类成员函数.

Likewise, if the top-level cv-qualification of the return type in the derived class member function does not match the top-level cv-qualification of the return type in the base class, the derived class member function does not override the base class member function.

D :: f 的返回类型中的类类型与b :: f的返回类型中的类类型具有相同的cv限定或更少的cv限定.

the class type in the return type of D::f has the same cv-qualification as or less cv-qualification than the class type in the return type of B::f.

这是指返回值的类类型的限定条件(即, const int * volatile 中的 const ).此规则意味着派生类成员函数中返回类型的限定必须等于或小于它所覆盖的基类成员函数的返回类型的限定.

This is referring to the qualification of the class type of the return value (that is, the const in const int* volatile). This rule means that the qualification of the return type in the derived class member function must be equal to or less than that of return type of the base class member function that it overrides.

这篇关于协变量返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆