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

  • SEARCH
  • Home
  • 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 7511853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:41:18+00:00 2026-05-29T23:41:18+00:00

For some syntactic sugar I want to return a reference to this , but

  • 0

For some syntactic sugar I want to return a reference to this, but when inherited, the function should return the type of the child class:

class base {
  T &operator!() { return *this; }
};
base b; b = !b;

class child : public base {};
child c; c = !c;

Because of the operator, I can’t just return the pointer and dynamic_cast it, it has to be a reference.

Is that even possible? Using decltype(*this) for T doesn’t work, neither does auto f()->decltype(*this), because of this (although I don’t understand why, in the auto-case)

In Scala you can write something like:

template<typename T> class base {
  T &f() { return *this; }
};
class child : public base<child> {};

But my g++ won’t accept this (not sure if that’s bug or just not in the spec?)

Of course there’s the explicit way, but I wonder if this can be avoided using C++11 features?

class child : public base {
  child &operator!() { base::operator!(); return *this }
};
  • 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-05-29T23:41:19+00:00Added an answer on May 29, 2026 at 11:41 pm

    You can use the CRTP to do this if you’re allowed to make base a template:

    template <typename Derived> class Base {
    protected:
        Derived& refToThis() {
            return *static_cast<Derived*>(this);
        }
    };
    

    Note the extra cast here. The reason this works is that if you have a class like this one:

    class Subclass: public Base<Subclass> {
        /* ... */
    };
    

    Then if you call refToThis from inside that class, it will call the base class version. Since the class inherits from Base<Subclass>, the instantiated template for refToThis will be

        Subclass& refToThis() {
            return *static_cast<Subclass*>(this);
        }
    

    This code is safe, because the this pointer does indeed point to a Subclass object. Moreover, the static_cast will ensure that the cast fails at compile-time if the derived class doesn’t inherit from Base properly, as the pointer type won’t be convertible.

    The reason that the cast is necessary here is that if you just say

    template <typename Derived> class Base {
    protected:
        Derived& refToThis() {
            return *this;
        }
    };
    

    Then there is a type error in the program, since a Base by itself is not a Derived, and if you could convert a Base& into a Derived& without any checks you could break the type system.

    That said… I wouldn’t do this at all. Overloading operator! for this purpose makes the code less readable, and just writing *this is so idiomatic that hiding it will make your code much harder to understand. Using all of this template machinery to avoid something that’s common C++ seems misguided. If you’re doing something else before returning the reference that’s fine, but this just doesn’t seem like a good idea.

    Hope this helps!

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

Sidebar

Related Questions

I would like to add some syntactic sugar to a class by overloading ()
I need to add some lightweight syntactic sugar to JavaScript source code, and process
I don't know if this is possible, but in some of my unit tests,
Just want to make simple extension for syntactic sygar : public static bool IsNotEmpty(this
Is there some internal difference between the C# syntactic sugar way of making properties:
Some things are easier to implement just by hand (code), but some are easier
I wrote a grammar for a language and now I want to treat some
While I realize that Visual C++ is a language lacking much of the syntactic-sugar
Syntactic sugar, IMHO, generally makes programs much more readable and easier to understand than
Since the C# using statement is just a syntactic sugar for try/finally{dispose}, why does

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.