Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8108693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:11:34+00:00 2026-06-06T01:11:34+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:11:35+00:00Added an answer on June 6, 2026 at 1:11 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This confuses me. The following compiles fine under Eclipse. package com.example.gotchas; public class GenericHelper1
Ideally I want a function something like the following (this example doesn't compile): void
Visual studio 2008 isn't catching syntax errors. for example: Tpc_feed(void); compiles fine. this: Tpc_feed(void);;;;;;;
The following code does not compile with gcc 4.7 (20120114) , but compiles fine
This simple example fails to compile in VS2K8: io_service io2; shared_ptr<asio::deadline_timer> dt(make_shared<asio::deadline_timer>(io2, posix_time::seconds(20))); As
The following example will not compile for me: #include <iostream> #include <functional> #include <string>
I tried to compile this example: #include <stdio.h> #include <stdlib.h> #include <stddef.h> main(){ size_t
For example, I get this compiler warning, The event 'Company.SomeControl.SearchClick' is never used. But
This example seems to compile with VC10 and gcc (though my version of gcc
I am currently trying to get the following to compile: class foo { };

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.