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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:06:09+00:00 2026-05-24T05:06:09+00:00

Is auto x = initializer; equivalent to decltype(initializer) x = initializer; or decltype((initializer)) x

  • 0

Is

auto x = initializer;

equivalent to

decltype(initializer) x = initializer;

or

decltype((initializer)) x = initializer;

or neither?

  • 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-24T05:06:10+00:00Added an answer on May 24, 2026 at 5:06 am

    decltype also considers whether the expression is rvalue or lvalue .

    Wikipedia says,

    The type denoted by decltype can be different from the type deduced by auto.

    #include <vector>
    int main()
    {
        const std::vector<int> v(1);
        auto a = v[0];        // a has type int
        decltype(v[0]) b = 1; // b has type const int&, the return type of
                            // std::vector<int>::operator[](size_type) const
        auto c = 0;           // c has type int
        auto d = c;           // d has type int
        decltype(c) e;        // e has type int, the type of the entity named by c
        decltype((c)) f = c;  // f has type int&, because (c) is an lvalue
        decltype(0) g;        // g has type int, because 0 is an rvalue
    }
    

    That pretty much explains the imporant difference. Notice decltype(c) and decltype((c)) are not same!

    And sometime auto and decltype works together in a cooperative way, such as in the following example (taken from wiki, and modified a bit):

    int& foo(int& i);
    float foo(float& f);
    
    template <class T>
    auto f(T& t) −> decltype(foo(t)) 
    {
      return foo(t);
    }
    

    Wikipedia further explains the semantics of decltype as follows:

    Similarly to the sizeof operator, the operand of decltype is unevaluated. Informally, the type returned by decltype(e) is deduced as follows:

    • If the expression e refers to a variable in local or namespace scope, a static member variable or a function parameter, then the result is that variable’s or parameter’s declared type
    • If e is a function call or an overloaded operator invocation, decltype(e) denotes the declared return type of that function
    • Otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e; if e is an rvalue, the result is T

    These semantics were designed to fulfill the needs of generic library writers, while at the same time being intuitive for novice programmers, because the return type of decltype always matches the type of the object or function exactly as declared in the source code. More formally, Rule 1 applies to unparenthesized id-expressions and class member access expressions. For function calls, the deduced type is the return type of the statically chosen function, as determined by the rules for overload resolution. Example:

    const int&& foo();
    int i;
    struct A { double x; };
    const A* a = new A();
    decltype(foo()) x1; // type is const int&&
    decltype(i) x2; // type is int
    decltype(a->x) x3; // type is double
    decltype((a->x)) x4; // type is const double&
    

    The reason for the difference between the latter two invocations of decltype is that the parenthesized expression (a->x) is neither an id-expression nor a member access expression, and therefore does not denote a named object.Because the expression is an lvalue, its deduced type is “reference to the type of the expression”, or const double&.

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

Sidebar

Related Questions

I need to implement auto-capitalization inside of a Telerik RadEditor control on an ASPX
I am developing an auto-builder that will run a series of steps in our
We have an auto-complete list that's populated when an you send an email to
I'm trying to auto-generate a plain text email with a trademark symbol in it.
Many programs include an auto-updater, where the program occasionally looks online for updates, and
Saving and auto-filing of username/password is a feature of most modern browsers. And the
When using auto implemnted properies like public string MyProperty { get; set; } This
How does Stack Overflow auto-generate the gravatars for those users who do not upload
I would like to initialize several auto-vivifying hashes by one-line expression. So far I
I learned that auto-variables aren't initialized to zero. So the following code will behave

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.