Jump to page: 1 2
Thread overview
param2 = param1
Aug 26, 2013
Luís Marques
Aug 27, 2013
bearophile
Aug 27, 2013
Luís Marques
Aug 27, 2013
Jonathan M Davis
Aug 27, 2013
Walter Bright
Aug 27, 2013
Jonathan M Davis
Aug 27, 2013
Walter Bright
Aug 27, 2013
H. S. Teoh
Aug 27, 2013
Walter Bright
Aug 27, 2013
Timon Gehr
Aug 28, 2013
Tove
Aug 28, 2013
Jacob Carlborg
Aug 28, 2013
Walter Bright
Aug 29, 2013
Jacob Carlborg
Aug 27, 2013
Andrej Mitrovic
Aug 28, 2013
Jacob Carlborg
Aug 28, 2013
Yota
August 26, 2013
Is there a reason why template parameters can default to other template parameters, but not regular parameters? For instance:

    // OK
    int foo(int x, int y = x)()
    {
        return x*y;
    }

    // Not OK
    int foo(int x, int y = x)
    {
        return x*y;
    }

When I noticed this it seemed arbitrary, but maybe there's a reason?
August 27, 2013
Luís Marques:

> Is there a reason why template parameters can default to other template parameters, but not regular parameters? For instance:
>
>     // OK
>     int foo(int x, int y = x)()
>     {
>         return x*y;
>     }
>
>     // Not OK
>     int foo(int x, int y = x)
>     {
>         return x*y;
>     }
>
> When I noticed this it seemed arbitrary, but maybe there's a reason?

Do you have some use cases for this?

Bye,
bearophile
August 27, 2013
On Tuesday, 27 August 2013 at 11:09:01 UTC, bearophile wrote:
> Do you have some use cases for this?

No, not at the moment. I was just wondering if there was a reason for what seemed an arbitrary difference between template parameters and normal parameters.
August 27, 2013
On Tuesday, August 27, 2013 21:33:06 =?UTF-8?B?Ikx1w61z?=.Marques <luis@luismarques.eu>@puremagic.com wrote:
> On Tuesday, 27 August 2013 at 11:09:01 UTC, bearophile wrote:
> > Do you have some use cases for this?
> 
> No, not at the moment. I was just wondering if there was a reason for what seemed an arbitrary difference between template parameters and normal parameters.

Not that I'm aware of. I'd file a bug (or at least an enhancement request) on it on the grounds we should be consistent unless there's a good reason not to be, and I'm not aware of any reason for this particular inconsistency (though honestly, I wouldn't have expected it to work in either case - if it can, great, but I would have just assumed that it wouldn't).

- Jonathan M Davis
August 27, 2013
On 8/27/2013 12:47 PM, Jonathan M Davis wrote:
> On Tuesday, August 27, 2013 21:33:06 =?UTF-8?B?Ikx1w61z?=.Marques
> <luis@luismarques.eu>@puremagic.com wrote:
>> On Tuesday, 27 August 2013 at 11:09:01 UTC, bearophile wrote:
>>> Do you have some use cases for this?
>>
>> No, not at the moment. I was just wondering if there was a reason
>> for what seemed an arbitrary difference between template
>> parameters and normal parameters.

No reason. It simply never occurred to anyone. I've never heard of anyone wanting this in all my years of C, C++, and D.

> Not that I'm aware of. I'd file a bug (or at least an enhancement request) on
> it on the grounds we should be consistent unless there's a good reason not to
> be, and I'm not aware of any reason for this particular inconsistency (though
> honestly, I wouldn't have expected it to work in either case - if it can,
> great, but I would have just assumed that it wouldn't).

I'd reject an enhancement request for this unless someone could demonstrate significant utility for it.
August 27, 2013
On Tuesday, August 27, 2013 12:56:12 Walter Bright wrote:
> > Not that I'm aware of. I'd file a bug (or at least an enhancement request) on it on the grounds we should be consistent unless there's a good reason not to be, and I'm not aware of any reason for this particular inconsistency (though honestly, I wouldn't have expected it to work in either case - if it can, great, but I would have just assumed that it wouldn't).
> 
> I'd reject an enhancement request for this unless someone could demonstrate significant utility for it.

I'd argue for it simply on the grounds that we try and be consistent, and having it work with function arguments and not template arguments is arbitrary and inconsistent. But other than that, I'm not sure that I care, as it's not something that I've ever needed to do with either function arguments or template arguments.

- Jonathan M Davis
August 27, 2013
On Tue, Aug 27, 2013 at 12:56:12PM -0700, Walter Bright wrote:
> On 8/27/2013 12:47 PM, Jonathan M Davis wrote:
> >On Tuesday, August 27, 2013 21:33:06 =?UTF-8?B?Ikx1w61z?=.Marques <luis@luismarques.eu>@puremagic.com wrote:
> >>On Tuesday, 27 August 2013 at 11:09:01 UTC, bearophile wrote:
> >>>Do you have some use cases for this?
> >>
> >>No, not at the moment. I was just wondering if there was a reason for what seemed an arbitrary difference between template parameters and normal parameters.
> 
> No reason. It simply never occurred to anyone. I've never heard of anyone wanting this in all my years of C, C++, and D.

TBH I've never heard of anyone wanting normal (non-template) parameters to default to another parameter either. D is the first language I know that has such a feature.


> >Not that I'm aware of. I'd file a bug (or at least an enhancement request) on it on the grounds we should be consistent unless there's a good reason not to be, and I'm not aware of any reason for this particular inconsistency (though honestly, I wouldn't have expected it to work in either case - if it can, great, but I would have just assumed that it wouldn't).
> 
> I'd reject an enhancement request for this unless someone could demonstrate significant utility for it.

What was the original rationale for normal parameters to be able to default to another parameter?


T

-- 
Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth? -- Michael Beibl
August 27, 2013
On 8/27/2013 1:05 PM, H. S. Teoh wrote:
> TBH I've never heard of anyone wanting normal (non-template) parameters
> to default to another parameter either. D is the first language I know
> that has such a feature.

It was implemented because it was necessary for many templates.

August 27, 2013
On 8/27/2013 1:04 PM, Jonathan M Davis wrote:
> I'd argue for it simply on the grounds that we try and be consistent, and
> having it work with function arguments and not template arguments is arbitrary
> and inconsistent. But other than that, I'm not sure that I care, as it's not
> something that I've ever needed to do with either function arguments or
> template arguments.

Above all, D needs to be useful. Implementing complex features that have no known purpose is counterproductive.

August 27, 2013
On 08/27/2013 09:56 PM, Walter Bright wrote:>
> No reason. It simply never occurred to anyone. ...

It's just too easy to work around using overloads in the usual case to be annoying enough for opening an enhancement/bug report.

>
> I'd reject an enhancement request for this

I guess then you'll see it as a non-standard extension at some point.
Arbitrary limitations tend to irk certain compiler developers more than others. :o)

> unless someone could demonstrate significant utility for it.

- Safe alloca wrapper using the alloca default argument hack together with this. (i.e. bearophile's dynamically-sized strongly typed stack-based arrays.)

- Default arguments based on aggregate members.

(This second use case, is just convenience, it avoids wrapper functions. However this is true for any, even existing, use case except using default arguments as a poor man's replacement for hygienic macros.)


( - Another plus is that the slight change of name lookup implied by this change is also handy for later introduction of dependent types. :-) )

« First   ‹ Prev
1 2