www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5347] New: Add constructors for primitive types to make pointers easier to use

reply d-bugmail puremagic.com writes:
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



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: -------
Dec 13 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5347


yebblies <yebblies gmail.com> changed:

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



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: -------
Dec 13 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5347


Jonathan M Davis <jmdavisProg gmx.com> changed:

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



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: -------
Dec 13 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5347






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: -------
Dec 13 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5347


bearophile_hugs eml.cc changed:

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




 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: -------
Dec 13 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5347


yebblies <yebblies gmail.com> changed:

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



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: -------
Jan 12 2013