Thread overview
[Issue 5347] New: Add constructors for primitive types to make pointers easier to use
Dec 13, 2010
Jonathan M Davis
Dec 13, 2010
yebblies
Dec 13, 2010
Jonathan M Davis
Dec 13, 2010
yebblies
Jan 13, 2013
yebblies
December 13, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5347

           Summary: Add constructors for primitive types to make pointers
                    easier to use
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: jmdavisProg@gmx.com


--- Comment #0 from Jonathan M Davis <jmdavisProg@gmx.com> 2010-12-13 00:10:50 PST ---
I'd like to be able to do something like this:

int* i = new int(7);


It would create a new pointer to int where the int that it points to has a value of 7. But that code results in the compiler complaining that there is no constructor for int, and as far as I can tell the only way to do it so the far less elegant and definitely less compact:

int* i = new int;
*i = 7;


Ideally, you'd be able to use the first syntax for all primitive types. As it is however, you can only use it for structs (and classes if you're using a reference rather than a pointer). This seems unnecessarily limiting. In fact, I thought that the first syntax _was_ legal, and it threw me off when it didn't work (just goes to show how rarely I use pointers, I guess).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 13, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5347


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies@gmail.com


--- Comment #1 from yebblies <yebblies@gmail.com> 2010-12-13 02:20:32 PST ---
This doesn't seem like something that would be used very often.

You could instead use

int* i = [7].ptr;

which should generate the same code, due to the way primitive types are allocated.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 13, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5347


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         OS/Version|Linux                       |All


--- Comment #2 from Jonathan M Davis <jmdavisProg@gmx.com> 2010-12-13 02:29:32 PST ---
Not useful very often? How about every time that you create a pointer to an int (or any other primitive type) and want to immediately initialize the value that's pointed to? It seems like it would be _highly_ useful whenever dealing with pointers to primitives. If it's not useful very often, it's because pointers aren't used very often.

And a syntax such as new int(7) or new bool(true) would be completely consistent with all of the other types. In fact, adding it would make the language _more_ consistent, not less. This would be particularly true when writing generic code.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 13, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5347



--- Comment #3 from yebblies <yebblies@gmail.com> 2010-12-13 02:45:34 PST ---
(In reply to comment #2)

I agree it would be more consistent that way, for value types.

void f(T)(T v)
{
   auto x = new T(v); // what if T is a class, array, static array etc?
}

A search of my own code doesn't come up with any instances of allocating a
single value type like this.
Searching phobos gives two instances, both in unittests.

If it's not used very often, what's the point of adding syntax for it?

Just saying.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 13, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5347


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #4 from bearophile_hugs@eml.cc 2010-12-13 04:01:46 PST ---
(In reply to comment #2)
> Not useful very often? How about every time that you create a pointer to an int (or any other primitive type) and want to immediately initialize the value that's pointed to?

The syntax is supported for structs and unions. So please show some use cases for ints, floats, etc.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 13, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=5347


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |DUPLICATE


--- Comment #5 from yebblies <yebblies@gmail.com> 2013-01-13 18:17:37 EST ---
Issue 9112 has a pull.

*** This issue has been marked as a duplicate of issue 9112 ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------