在decltype中使用this指针 [英] Using the this pointer inside decltype

查看:177
本文介绍了在decltype中使用this指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例(编译良好)

  struct A 
{
void f(){};
auto g() - > decltype(f())
{}

};

问题



如果我在decltype(即 decltype(this-> f())中添加 this 我得到以下编译错误与gcc 4.7.0:

 错误:无效使用不完整的类型'struct A'
错误:'struct A'的前向声明
错误:无效使用不完整的类型'struct A'
错误:正向声明'struct A'

使用这个不允许使用decltype吗?


$ b

href =http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53721 =nofollow>此问题已作为错误提交。


<看起来问题不是这个出现在 decltype 中

根据GCC 4.7编译:

  struct A 
{
int f(){return 0; }
auto g() - > decltype(f()){
decltype(this-> f())var = this-> f
return var;
}
};

这使用 decltype(this-> f()) 正文中 g ,但在 auto中指定函数的返回类型。 ... - .... 表单,即所谓的尾随返回类型规范不是函数体的一部分,GCC不允许

这个在函数体中使用:§5.1.1的标准规定这个可以在可选的const / volatile限定符和函数体的结束,见下面第3节。 (为了完整起见,我添加了第4条,其中涉及数据成员,并且与问题不直接相关。)


(条款3)如果一个声明声明了一个类X的成员函数或成员函数模板,那么表达式 this
是类型的指针cv-quali fi er-seq X 在可选cv-qualifer-seq和
函数定义,成员声明符或声明符的结尾之间。 它不会出现在可选的cv-qualiter-seq
之前,它不会出现在静态成员函数的声明中(尽管它的类型和值
类别定义在静态成员函数,因为它们在非静态成员函数内)。 [...]



(第4条)否则,如果成员声明者声明了一个非静态的数据成员类X,表达式
是可选的大括号初始化器中类型的指针X的prvalue。它不会出现在其他地方
在成员-declarator。



(第5条)表达式不应出现在任何其他上下文中。 [...]


注意:可选cv-qualifier-seq $ c> const 或 volatile 函数的限定符必须在注释中指出, 因此使用这个在问题中描述的方式应该是正确的,和 GCC似乎是错误。 / strong>


Example (compiles fine)

struct A
{
    void f() {};
    auto g() -> decltype(f())
    {}

};

Question

If I add the this pointer inside decltype (i.e. decltype(this->f())), I get the following compile errors with gcc 4.7.0:

error: invalid use of incomplete type 'struct A'
error: forward declaration of 'struct A'
error: invalid use of incomplete type 'struct A'
error: forward declaration of 'struct A'

Is using this in decltype not allowed? Could someone help me understand what is the problem?

EDIT

This has been filed as a bug.

解决方案

It seems the problem isn't that this appears inside a decltype, but that it appears outside the function body.

For example, this code below compiles under GCC 4.7:

struct A
{
  int f() { return 0; }
  auto g() -> decltype(f()) {
    decltype(this->f()) var = this->f();
    return var;
  }
};

This uses decltype(this->f()) inside the body of g, but the specification of the return type of the function in the auto .... -> .... form, i.e. the so-called trailing return type specification, is not part of the function body, and GCC does not allow it there.

However, it would appear (see discussion in comments) that the C++ Standard does not actually require this to be used in the function body: §5.1.1 of the standard states that this may be used anywhere between the optional const/volatile qualifier and the end of the function body, see clause 3 below. (For the sake of completeness, I have added clause 4 as well, which talks about data members and is not directly relevant to the question).

(Clause 3) If a declaration declares a member function or member function template of a class X, the expression this is a prvalue of type "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq and the end of the function-definition, member-declarator, or declarator. It shall not appear before the optional cv-qualifier-seq and it shall not appear within the declaration of a static member function (although its type and value category are defined within a static member function as they are within a non-static member function). [...]

(Clause 4) Otherwise, if a member-declarator declares a non-static data member (9.2) of a class X, the expression this is a prvalue of type "pointer to X" within the optional brace-or-equal-initializer. It shall not appear elsewhere in the member-declarator.

(Clause 5) The expression this shall not appear in any other context. [...]

NB: The optional cv-qualifier-seq, i.e. the const or volatile qualifier for the function must, as Jesse points out in the comment, appear before the trailing return type declaration. Hence using this in the way described in the question should be correct, and GCC seems to be wrong.

这篇关于在decltype中使用this指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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