Home Comments

Parameterized constructors for structs?

shavershaver Member ✭✭
It would be nice if slice2cpp would generate a basic parameterized constructor for structs, such that
struct Position {
    int x;
    int y;
    int z;
}
could be instantiated in C++ with something like
Position p(1, 2, 3);
instead of having to spell it out field-by-field as
Position p;
p.x = 1;
p.y = 2;
p.z = 3;

Mike

Comments

  • michimichi Member Michi HenningOrganization: Triodia TechnologiesProject: I have a passing interest in Ice :-) ✭✭✭
    Re: Parameterized constructors for structs?
    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:
    struct Position p = { 1, 2, 3 };
    

    Cheers,

    Michi.
  • shavershaver Member ✭✭
    Re: Re: Parameterized constructors for structs?
    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!
    Incidentally, because the structure members are public, you can statically initialize them:
    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
  • michimichi Member Michi HenningOrganization: Triodia TechnologiesProject: I have a passing interest in Ice :-) ✭✭✭
    Re: Re: Re: Parameterized constructors for structs?
    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).
    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:
    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.
Sign In or Register to comment.