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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:47:17+00:00 2026-06-08T21:47:17+00:00

For example, I have some class DataPacket . What is the difference between: auto

  • 0

For example, I have some class DataPacket. What is the difference between:

auto packet = DataPacket(); 

and

DataPacket packet;

?

  • 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-08T21:47:21+00:00Added an answer on June 8, 2026 at 9:47 pm

    To answer the question about auto first, there is no difference in the generated code between:

    auto packet = DataPacket(); 
    

    and

    DataPacket packet = DataPacket();
    

    But that’s not what you wrote.

    In the original question, the first one creates a value-initialized temporary and then copy-initializes packet from it. That requires an accessible, non-explicit copy or move constructor, requires the type can be default-constructed, and ensures packet is initialized (assuming the copy/move constructor isn’t buggy.) The second one default-initializes packet which only requires that the type can be default-constructed, but leaves the object uninitialized if it has a trivial default constructor, for example:

    struct DataPacket { int i; };
    {
      DataPacket packet = DataPacket();
      ++packet.i;  // OK
    }
    {
      DataPacket packet;
      ++packet.i;  // undefined behaviour
    }
    

    As Xeo points out in a comment below, these is less difference between these:

    auto packet = DataPacket();
    
    DataPacket packet{};
    

    because the second of those also ensures value-initialization, so in that case the difference is that the former requires an accessible, non-explicit copy or move constructor.

    In all the cases that require an accessible copy/move constructor, if the copy (or move) isn’t elided then the code generated will be different because of the copy/move. But in practice it will be elided by all modern compilers so the generated code will be identical.

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

Sidebar

Related Questions

What is the difference between proxy and reify? I have some example code: (.listFiles
For example, I have some class hierarchy (possibly, with all kinds of inheritance -
If say I have some generic class, for example: public class Attribute<T> { }
if I have for example some class Base and derived from it Derived Also
Let me give example: I have some generic class/interface definition: interface IGenericCar< T >
I have some questions regarding java finalization. For example I have one class FileHelper
For example I have this: class A{ private int mine = 0; //Some field...
For example, I have some class .article, and I want to view this class
I have some class, for example A , and others who extended A (A1,A2)
I have some class. For example: public class Data { private String name; public

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.