Go Back   ZeroC Forums > Comments

Reply
 
LinkBack Thread Tools Rate Thread Display Modes
  #1 (permalink)  
Old 01-10-2004
shaver shaver is offline
Registered User
 
 
Join Date: May 2003
Posts: 35
Parameterized constructors for structs?

It would be nice if slice2cpp would generate a basic parameterized constructor for structs, such that
Code:
struct Position {
    int x;
    int y;
    int z;
}
could be instantiated in C++ with something like
Code:
Position p(1, 2, 3);
instead of having to spell it out field-by-field as
Code:
Position p;
p.x = 1;
p.y = 2;
p.z = 3;
Mike
Reply With Quote
  #2 (permalink)  
Old 01-10-2004
michi's Avatar
michi michi is offline
ZeroC Staff
 
Name: Michi Henning
Organization: ZeroC
Project: Ice
 
Join Date: Feb 2003
Location: Brisbane, Australia
Posts: 896
Re: Parameterized constructors for structs?

Quote:
Originally posted by shaver
It would be nice if slice2cpp would generate a basic parameterized constructor for structs
I considered this for structures, non-abstract classes, and exceptions, but the gain seems to be minor: we'd end up generating additional code for every structure, class, and exception when, in many cases, that code would never be called and, therefore, would serve only to bloat the process and add to its working set size. So, on balance, I suspect that the minor added convenience isn't worth the penalty.

Incidentally, because the structure members are public, you can statically initialize them:
Code:
struct Position p = { 1, 2, 3 };
Cheers,

Michi.
Reply With Quote
  #3 (permalink)  
Old 01-10-2004
shaver shaver is offline
Registered User
 
 
Join Date: May 2003
Posts: 35
Re: Re: Parameterized constructors for structs?

Quote:
Originally posted by michi
I considered this for structures, non-abstract classes, and exceptions, but the gain seems to be minor: we'd end up generating additional code for every structure, class, and exception when, in many cases, that code would never be called and, therefore, would serve only to bloat the process and add to its working set size. So, on balance, I suspect that the minor added convenience isn't worth the penalty.
I guess I was thinking of inline constructors, such that they would become simply a bit of syntactic sugar, and wouldn't incur any penalty for programs that didn't use them. I do appreciate your desire to keep the generated code's size down, though!
Quote:
Incidentally, because the structure members are public, you can statically initialize them:
Code:
struct Position p = { 1, 2, 3 };
Very true, though using that sort of static initialization as part of an expression (for me, commonly, function arguments) makes for some ugly typing.

Mike
Reply With Quote
  #4 (permalink)  
Old 01-10-2004
michi's Avatar
michi michi is offline
ZeroC Staff
 
Name: Michi Henning
Organization: ZeroC
Project: Ice
 
Join Date: Feb 2003
Location: Brisbane, Australia
Posts: 896
Re: Re: Re: Parameterized constructors for structs?

Quote:
Originally posted by shaver
I guess I was thinking of inline constructors, such that they would become simply a bit of syntactic sugar, and wouldn't incur any penalty for programs that didn't use them. I do appreciate your desire to keep the generated code's size down, though!
Well, even if the constructor is defined in-line, there is no guarantee that the compiler will actually inline it, so it may still result in dead code. In fact, with older linkers that do not eliminate duplicate inline functions, having the inline constructor can result in getting a separate copy in every object file that includes the header file in which the structure is defined (although, I suspect that pretty much all linkers these days are smart enough to eliminate the duplicates).

Quote:
Very true, though using that sort of static initialization as part of an expression (for me, commonly, function arguments) makes for some ugly typing.
Yes, that doesn't look too pretty... If you are really keen on having the constructor, you can derive a struct from the one that is generated by the slice2cpp compiler and define the constructor for that:
Code:
struct MyPosition : public Position {
    MyPosition(int x, int y, int z) : Position(x, y, z) {}
};

foo(MyPosition(a, b, c));
I think that, overall, this solution is preferable, given that it is trivial to implement.

Cheers,

Michi.
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Latency: structs vs. classes n2503v Help Center 7 10-09-2005 12:08 AM
generated C# classes don't have constructors with arguments for fields kovacm Bug Reports 1 08-07-2005 07:00 PM
C#: Attributes with Ice generated classes and structs DeepDiver Help Center 2 07-23-2005 02:16 AM
Feature Request: generate constructors with parameter for initialization DeepDiver Comments 2 01-04-2005 10:05 AM
Java uint tests don't test a seq of structs mdoar Comments 4 01-21-2004 06:00 PM


All times are GMT -4. The time now is 01:05 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
(c) 2008 ZeroC, Inc.