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 6856689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:55:05+00:00 2026-05-27T01:55:05+00:00

For some template typename, I want to make a typedef which is the declared

  • 0

For some template typename, I want to make a typedef which is the declared return type of T::operator++() (aka T’s preincrement operator).

I didn’t find anything definitive online, though there are certainly some mentions of decltype with preincrement. So I tried a few things, and the only one which really works seems like a dirty hack. What do you think of this?

struct S { // dummy type to simulate a real one I have
    int operator++() { return 0; } // note: return type is not S&
    int operator++(int) { return 0; }
};

int main() {
    // this works:
    typedef decltype(++S()) T1;

    // so why doesn't this work?
    // error: lvalue required as increment operand
    // typedef decltype(++int()) T2;

    // this works, but seems dirty:
    typedef decltype(++*(int*)nullptr) T3;
    typedef decltype(++*(S*)nullptr) T4;

    // I also haven't figured out how to disambiguate this,
    // though it's moot because int::operator++ is not a thing
    // error: ‘S::operator++’ refers to a set of overloaded functions
    // typedef decltype(S::operator++) T5;
}

I’m using GCC 4.6.2. I briefly tried Clang but it was no better.

  • 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-27T01:55:06+00:00Added an answer on May 27, 2026 at 1:55 am

    The lvalueness of builtin and user-defined types differs in the case of temporaries: a temporary int in your example is an rvalue, but a temporary S is an lvalue.

    Edit: Technically, all temporaries are rvalues, but operators work differently with user-defined types because they are actually regular functions in disguise. This means you can do some rather non-rvalue-like things with them, such as having an S() as the left-hand side of the default assignment operator!

    Use declval to get an lvalue or rvalue of an arbitrary type in a non-evaluated context:

    #include <utility>
    
    // declval<T&> yields an lvalue, declval<T> an rvalue
    typedef decltype(std::declval<int&>()++) T1; // int
    typedef decltype(++std::declval<int&>()) T2; // int&
    
    typedef decltype(std::declval<S&>()++)   T3; // S
    typedef decltype(++std::declval<S&>())   T4; // S&
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently, I have some code as follows template<typename Type> Type* getValue(std::string name, bool tryUseGetter
I want do make some classes, which will have private/protected constructors, and will have
I want to store such structure template <typename var_type> struct { int some_var; //...some
Here's some code (full program follows later in the question): template <typename T> T
For some complicated reason, I want to convert any supported type T (coming from
I'm writing a simple maths library with a template vector type: template<typename T, size_t
I want to write a template function that checks some Timestamp property (class inherits
I want to specialize following member function: class foo { template<typename T> T get()
I want to create a function like: template < typename Other, typename Func, typename
Please, consider the code below: template<typename T> bool function1(T some_var) { return true; }

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.