www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1860] New: Heap construction of structs with args doesn't work

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1860

           Summary: Heap construction of structs with args doesn't work
           Product: D
           Version: 1.027
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: wbaxter gmail.com


Heap construction of structs using arguments doesn't work.  It doesn't matter
if there's a static opCall explicitly defined or not.

-----
struct NoOpCalls
{
    int i;
    float x;
}
struct WithOpCall
{
    static WithOpCall opCall(int _i, float _x) {
        WithOpCall R; with(R) {
            i = _i;
            x = _x;
        } return R;
    }

    int i;
    float x;
}


void main()
{
    auto stacka = NoOpCalls(2, 5.0); // ok
    auto stackb = WithOpCall(2, 5.0); // ok

    auto heapa = new NoOpCalls(2, 5.0); // error no constructor
    auto heapb = new WithOpCall(2, 5.0); // error no constructor
}
----

This seems like an oversight rather than a deliberate design decision.

You can work around by "double initializing":

   auto heapa = new NoOpCalls; *heapa = NoOpCalls(2,5.0);

But that seems unnecessarily repetitive.

Anyway, this is yet another reason why structs should have real constructors,
as opposed to the static opCall hack.


-- 
Feb 21 2008
next sibling parent reply downs <default_357-line yahoo.de> writes:
Try this.

import std.gc
struct heapmalloc(T) {
	static T* opCall(U...)(U u) {
		auto res = cast(T*) malloc(T.sizeof);
		(*res) = T(u);
		return res;
	}
}


struct Foo {
	int a;
	float b;
}

void main() {
	auto heapfoo = heapmalloc!(Foo)(1984, 3.14159);
}
Feb 21 2008
parent reply downs <default_357-line yahoo.de> writes:
downs wrote:
 Try this.
 
 import std.gc;
 struct heapmalloc(T) {
 	static T* opCall(U...)(U u) {
 		auto res = cast(T*) malloc(T.sizeof);
 		(*res) = T(u);
 		return res;
 	}
 }
 
 
 struct Foo {
 	int a;
 	float b;
 }
 
 void main() {
 	auto heapfoo = heapmalloc!(Foo)(1984, 3.14159);
 }

Or this. import std.gc; T* toHeap(T)(T st) { auto res = cast(T*) malloc(T.sizeof); (*res) = st; return res; } struct Foo { int a; float b; } void main() { auto th = toHeap(Foo(1984, 3.1415926)); assert(th.a == 1984); } :) --downs
Feb 21 2008
parent downs <default_357-line yahoo.de> writes:
downs wrote:
 downs wrote:
 Try this.

 import std.gc;
 struct heapmalloc(T) {
 	static T* opCall(U...)(U u) {
 		auto res = cast(T*) malloc(T.sizeof);
 		(*res) = T(u);
 		return res;
 	}
 }


 struct Foo {
 	int a;
 	float b;
 }

 void main() {
 	auto heapfoo = heapmalloc!(Foo)(1984, 3.14159);
 }

Or this. import std.gc; T* toHeap(T)(T st) { auto res = cast(T*) malloc(T.sizeof); (*res) = st; return res; } struct Foo { int a; float b; } void main() { auto th = toHeap(Foo(1984, 3.1415926)); assert(th.a == 1984); } :) --downs

Or this! template heap() { import std.gc; typeof(this) heap() { auto res = cast(typeof(this)) malloc(typeof(*this).sizeof); (*res) = *this; return res; } } struct Foo { int a; float b; mixin heap!(); } void main() { auto th = Foo(1984, 3.1415926).heap; assert(th.a == 1984); }
Feb 21 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1860


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement




------- Comment #4 from bugzilla digitalmars.com  2008-03-02 21:56 -------
Marked as enhancement request.


-- 
Mar 02 2008