struct declaration and definition inside a parameter list

This is a piece of code I wrote to demonstrate passing structs as parameters.  The catch is that they are declared WITHIN the parameter list.  Which are not valid, and why they are not valid?  

Context - two compilers that I'm using can't seem to agree what is proper syntax.  One throws syntax errors.  My guess is that the one of the compilers have a bug in it.

CODE:

#include <iostream.h>

struct three_integer_struct
{
      int a;
      int b;
      int c;

      three_integer_struct
      (
            int z_a,
            int z_b,
            int z_c
      ) :
      a( z_a ),
      b( z_b ),
      c( z_c ) {};
};

void display ( three_integer_struct & data )
{
      cout << data.a <<endl <<data.b <<endl <<data.c <<endl;
}

void main()
{
      display ( (three_integer_struct) {1,2,3} );                  // Is this valid?
      display ( three_integer_struct {4,5,6} );                  // Is this valid?
      display ( three_integer_struct hello = {7,8,9} );      // Is this valid?
      display ( three_integer_struct hello {10,11,12} );      // Is this valid?
      display ( three_integer_struct (13,14,15) );            // This is valid.
}
LVL 1
runxctryAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>>       display ( (three_integer_struct) {1,2,3} );                  // Is this valid?

No. You can't initialize something without telling the compiler what it is, and then try to cast it.

>>       display ( three_integer_struct {4,5,6} );                  // Is this valid?

No. This is syntactically incorrect.

>>       display ( three_integer_struct hello = {7,8,9} );      // Is this valid?

No. You can't initialize with {} when you provide a constructor. Plus you can't create the variable like that inside the function call parameters.

>>       display ( three_integer_struct hello {10,11,12} );      // Is this valid?

No. This is syntactically incorrect.

>>       display ( three_integer_struct (13,14,15) );            // This is valid.

No. You can't pass a reference to a temporary value.


Here's a correct way to do what you want :

      three_integer_struct hello(13,14,15);
      display(hello);
0
Infinity08Commented:
Of course, if you modify the display function like this (ie. do not pass by reference but by value) :

      void display ( three_integer_struct data )
      {
            cout << data.a <<endl <<data.b <<endl <<data.c <<endl;
      }

then you can call it like this :

      display(three_integer_struct(13,14,15));




Or you create the function like this (ie. pass by pointer) :

      void display ( three_integer_struct *data )
      {
            cout << data->a <<endl <<data->b <<endl <<data->c <<endl;
      }

then you can call it like this :

      three_integer_struct hello(13,14,15);
      display(&hello);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Deepu AbrahamR & D Engineering ManagerCommented:
This will also work:

void display ( three_integer_struct * data )
{
      cout << data->a <<endl <<data->b <<endl <<data->c <<endl;
}

void main()
{
      display ( new three_integer_struct (13,14,15) );            // This is valid.
}
Best Regards,
DeepuAbrahamK
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Infinity08Commented:
>>       display ( new three_integer_struct (13,14,15) );            // This is valid.

Nooooooo !!

Memory leak alert :)
0
rstaveleyCommented:
>      display ( three_integer_struct (13,14,15) );

That would only be OK, if you made display take a const reference as its parameter.

i.e.
--------8<--------
void display (const three_integer_struct & data )
{
      cout << data.a <<endl <<data.b <<endl <<data.c <<endl;
}
--------8<--------

You can pass a reference to a const temporary object.
0
runxctryAuthor Commented:
Thanks, Infinity08 --

==
>>       display ( three_integer_struct hello = {7,8,9} );      // Is this valid?

No. You can't initialize with {} when you provide a constructor. Plus you can't create the variable like that inside the function call parameters.
==
You're right about the constructor thing.  Let's say I didn't provide one.

==
>>       display ( three_integer_struct (13,14,15) );            // This is valid.

No. You can't pass a reference to a temporary value.
==
In this case, I'm actually passing a temporary value to a reference.  This seems valid, especially since the display(...) function will consider the data as local scope.  Only once the flow of control passes this line (in main) will the temporary go out of scope.

Thanks again for your help.
0
runxctryAuthor Commented:
Thanks, rstavely.

Is there anything syntactically or logically incorrect about passing the DATA non-const, in your example (not the reference)?  It seems like it should work either way to me.

Infinity - my goal is to keep it a temporary to the scope of that one line, not the whole function.  Thanks for the memory leak comment - I seriously wouldn't have caught that.
0
runxctryAuthor Commented:
Upping the point value...  Will increase more as discussion increases and no answers (yep - subjective, sorry) present themselves.
0
Infinity08Commented:
>> You're right about the constructor thing.  Let's say I didn't provide one.
Then you could do something like this :

      #include <iostream>

      using namespace std;

      struct three_integer_struct
      {
            int a;
            int b;
            int c;
      };

      void display ( three_integer_struct data )
      {
            cout << data.a <<endl <<data.b <<endl <<data.c <<endl;
      }

      int main(void)
      {
            display ((three_integer_struct) {7,8,9});
            return 0;
      }

Note that I used <iostream> instead of <iostream.h>. The latter should NOT be used.


>> In this case, I'm actually passing a temporary value to a reference.
No, you're taking the reference of a temporary value, and pass that as parameter to the function. If you want to use references, make it a const reference like rstaveley suggested.

>> Only once the flow of control passes this line (in main) will the temporary go out of scope.
That doesn't matter. It's not allowed by the C++ standard.
0
Infinity08Commented:
>> my goal is to keep it a temporary to the scope of that one line, not the whole function.

Then either use rstaveley's const reference solution, or my pass-by-value solution.
0
runxctryAuthor Commented:
Infinity08 and rstavely --

===
No, you're taking the reference of a temporary value, and pass that as parameter to the function. If you want to use references, make it a const reference like rstaveley suggested.
===
Yes, you are right.  Thank you.  From Stroustrup, Section 5.5, "References" --
" References to variables and references to constants are distinguished because introducing a temporary for a variable would have been highly error-prone; an assignment to the variable would become an assignment to the - soon to disappear - temporary.  No such problem exists for references to constants, and references to constants are often important as function arguments."

Infinity08  - regarding your code  (thanks for that by the way) I have your own words to reply to you.  
" You can't initialize something without telling the compiler what it is, and then try to cast it. "
0
Infinity08Commented:
>> " You can't initialize something without telling the compiler what it is, and then try to cast it. "
That was when you added a constructor to the struct. Without a constructor, the compiler is intelligent enough to understand what you want to do.
My apologies for the confusion.
0
runxctryAuthor Commented:
Infinity and rstavely -- thank you both very much for your help.  I appreciate your time, and this was a great thing I learned today.  I now feel that I haven't given either of you enough points for your time and I would be pleased to give more, except I don't know how to do that.

About me - I'm a fairly junior engineer, still learning the ropes, so your wisdom is infinitely appreciated.
0
Infinity08Commented:
No need for more points, runxctry. I'm just glad I could be of assistance :)

Good luck (and hopefully also a lot of fun) in learning the inner workings of C++ - it will help you a lot in your further career !

If you have any questions and/or problems, feel free to ask !
0
rstaveleyCommented:
That was a vibrant Q&A. I'm sorry it caught me at the end of my shift. I can't improve on the wisdom of Stroustrup. Good luck, runxctry, and many thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.