none
Is there a difference between initializing an empty struct with parenthesis and with curly brackets? RRS feed

  • Question

  • I have some functions that sometimes return empty structs. When I do that I need to put either parenthesis or curly brackets after the type name in the return statement. Both ways yield the same result in what I am doing. But I want to know if difference I am not seeing in my simple example that could cause unexpected behavior down the line. 

    Here is an example:

    #include <iostream>
    
    struct MyStruct
    {
        int a=1;
        int b=2;
    };
    
    // with parenthesis
    MyStruct F1(void)
    {
        return MyStruct();
    }
    
    // with curly brackets
    MyStruct F2(void)
    {
        return MyStruct{};
    }
    
    int main(void)
    {
        MyStruct s1 = F1();
        MyStruct s2 = F2();
        
        std::cout << "s1: " << "a=" << s1.a << ", b=" << s1.b << std::endl;
        std::cout << "s2: " << "a=" << s2.a << ", b=" << s2.b << std::endl;   
    
        return 0;
    }

    So what I want to know is if this

    return MyStruct();

    differ from this

    return MyStruct{};
    in any way.


    • Edited by Lord Wolfenstein Thursday, August 15, 2019 7:52 PM changed the code to make a followup question relevant.
    Tuesday, August 13, 2019 9:02 AM

Answers

  • These behave the same.

    When you use () or {} on a class or struct without a user provided constructor (i.e. the default constructor is the compiler provided one) then by the C++ 11 rules the object is zero initialised.

    The important thing to note is that the class/struct cannot have any user specified default constructor.

    For example:

    struct s
    {
    	int a;
    	int b;
    };

    or:

    struct s
    {
    	s() = default;
    	s(int n_a, int n_b) : a(n_a), b(n_b) {}
    	int a;
    	int b;
    };

    will zero initialise, but:

    struct s
    {
    	s() {};
    	s(int n_a, int n_b) : a(n_a), b(n_b) {}
    	int a;
    	int b;
    };

    will default initialise.

    In the case where you provide the values in the class, then the compiler will use these values regardless of if you use {} or ().


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Thursday, August 15, 2019 8:14 PM

All replies

  • If you run the code, do you see any difference?
    Tuesday, August 13, 2019 9:28 PM
  • return MyStruct{};
    What is that? What gave you the idea to use that? Wherever you saw that, certainly there is something saying what it is. Perhaps it is called brace initialization or list initialization but whatever it is called, you should look for more information on whatever it is called. Then when you have questions, link to the article(s) you need help with.


    Sam Hobbs
    SimpleSamples.Info

    Tuesday, August 13, 2019 11:46 PM
  • Hi,

    Thank you for posting here.

    >>Is there a difference between initializing an empty struct with parenthesis and with curly brackets?

    return MyStruct{}; //Since C++11 accept this syntax
    return MyStruct(); //Zero initialize using default constructor

    Best Regards,

    Jeanine Zhang

    • Proposed as answer by Guido Franzke Wednesday, August 14, 2019 5:59 AM
    Wednesday, August 14, 2019 2:55 AM
    Moderator
  • Hi,

    Thank you for posting here.

    >>Is there a difference between initializing an empty struct with parenthesis and with curly brackets?

    return MyStruct{}; //Since C++11 accept this syntax
    return MyStruct(); //Zero initialize using default constructor

    Best Regards,

    Jeanine Zhang

    So the answer is "There is no difference" then.
    Wednesday, August 14, 2019 9:38 AM
  • Does the fact the value of an uninitialized object (one without a default constructor) is indeterminate have any bearing on your question?
    Wednesday, August 14, 2019 6:10 PM
  • Does the fact the value of an uninitialized object (one without a default constructor) is indeterminate have any bearing on your question?

    If they behave differently in any way then yes. Do they behave different?

    I also edited the code in the question to give the struct default values and the two ways act the same.

     
    Thursday, August 15, 2019 7:56 PM
  • I suggest you open the disassembly window in the debugger and step through the code for both F1() and F2().

    See if the compiler has generated identical assembly code for the return value from both functions.

    It's not clear if by "difference" if you mean syntactically at the source code level or the resultant code generated by the compiler.

    Thursday, August 15, 2019 8:05 PM
  • These behave the same.

    When you use () or {} on a class or struct without a user provided constructor (i.e. the default constructor is the compiler provided one) then by the C++ 11 rules the object is zero initialised.

    The important thing to note is that the class/struct cannot have any user specified default constructor.

    For example:

    struct s
    {
    	int a;
    	int b;
    };

    or:

    struct s
    {
    	s() = default;
    	s(int n_a, int n_b) : a(n_a), b(n_b) {}
    	int a;
    	int b;
    };

    will zero initialise, but:

    struct s
    {
    	s() {};
    	s(int n_a, int n_b) : a(n_a), b(n_b) {}
    	int a;
    	int b;
    };

    will default initialise.

    In the case where you provide the values in the class, then the compiler will use these values regardless of if you use {} or ().


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Thursday, August 15, 2019 8:14 PM