Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 17, 2014 default args for templates | ||||
---|---|---|---|---|
| ||||
Attachments:
| Observing strange behaviour, just want to know if it's correct... struct VertexBuffer(VertexDataType = void) {} VertexBuffer x; Error: struct VertexBuffer(VertexDataType = void) is used as a type auto y = VertexBuffer(); Error: struct VertexBuffer(VertexDataType = void) cannot deduce template function from argument types !()() But this works: VertexBuffer!() z; Why should I need to supply an empty argument list? This defeats the purpose of the default arg, and obscures my code in the common case. Untyped vertex buffers are the default, and it would be nice to be able to declare them trivially. Also, those error messages are quite unhelpful. |
January 17, 2014 Re: default args for templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On 2014-01-17 13:18, Manu wrote: > Observing strange behaviour, just want to know if it's correct... > > struct VertexBuffer(VertexDataType = void) {} > > VertexBuffer x; > Error: struct VertexBuffer(VertexDataType = void) is used as a type > > auto y = VertexBuffer(); > Error: struct VertexBuffer(VertexDataType = void) cannot deduce > template function from argument types !()() > > But this works: > VertexBuffer!() z; > > > Why should I need to supply an empty argument list? This defeats the > purpose of the default arg, and obscures my code in the common case. > > Untyped vertex buffers are the default, and it would be nice to be able > to declare them trivially. > > Also, those error messages are quite unhelpful. Yeah, that's a really annoying limitation. I'm wonder if there's a bugzilla entry for it. -- /Jacob Carlborg |
January 17, 2014 Re: default args for templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | Jacob Carlborg:
> Yeah, that's a really annoying limitation. I'm wonder if there's a bugzilla entry for it.
If the instantiation is automatic, then what's the literal to specify the un-instantiated template?
Bye,
bearophile
|
January 17, 2014 Re: default args for templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On Friday, 17 January 2014 at 12:18:51 UTC, Manu wrote:
> Observing strange behaviour, just want to know if it's correct...
>
> struct VertexBuffer(VertexDataType = void) {}
>
> VertexBuffer x;
> Error: struct VertexBuffer(VertexDataType = void) is used as a type
>
> auto y = VertexBuffer();
> Error: struct VertexBuffer(VertexDataType = void) cannot deduce template
> function from argument types !()()
>
> But this works:
> VertexBuffer!() z;
>
>
> Why should I need to supply an empty argument list? This defeats the
> purpose of the default arg, and obscures my code in the common case.
>
> Untyped vertex buffers are the default, and it would be nice to be able to
> declare them trivially.
>
> Also, those error messages are quite unhelpful.
There's enough subtlety in function identifiers vs function results due to optional parenthesis, I'd rather not have the same for templates too. Even if it could be made unambiguous, it would likely get pretty confusing.
|
January 17, 2014 Re: default args for templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 2014-01-17 13:53, bearophile wrote: > If the instantiation is automatic, then what's the literal to specify > the un-instantiated template? Perhaps based on the context, but that might be a bad idea. -- /Jacob Carlborg |
January 17, 2014 Re: default args for templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Friday, 17 January 2014 at 13:40:05 UTC, Jacob Carlborg wrote:
> On 2014-01-17 13:53, bearophile wrote:
>
>> If the instantiation is automatic, then what's the literal to specify
>> the un-instantiated template?
>
> Perhaps based on the context, but that might be a bad idea.
I think it is impossible in general case:
pragma(msg, StructSymbol.stringof); // "StructSymbol" or "StructSymbol!(void)" ?
Add aliases on top of it and it quickly gets messy.
|
January 17, 2014 Re: default args for templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile Attachments:
| On 17 January 2014 22:53, bearophile <bearophileHUGS@lycos.com> wrote:
> Jacob Carlborg:
>
>
> Yeah, that's a really annoying limitation. I'm wonder if there's a
>> bugzilla entry for it.
>>
>
> If the instantiation is automatic, then what's the literal to specify the un-instantiated template?
>
The context gives everything it needs. It's clearly being used to declare a variable.
|
January 17, 2014 Re: default args for templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | On Friday, 17 January 2014 at 12:18:51 UTC, Manu wrote:
> Observing strange behaviour, just want to know if it's correct...
>
> struct VertexBuffer(VertexDataType = void) {}
>
> VertexBuffer x;
> Error: struct VertexBuffer(VertexDataType = void) is used as a type
>
> auto y = VertexBuffer();
> Error: struct VertexBuffer(VertexDataType = void) cannot deduce template
> function from argument types !()()
>
> But this works:
> VertexBuffer!() z;
>
>
> Why should I need to supply an empty argument list? This defeats the
> purpose of the default arg, and obscures my code in the common case.
>
> Untyped vertex buffers are the default, and it would be nice to be able to
> declare them trivially.
>
> Also, those error messages are quite unhelpful.
The funny part though is that it works if the template is a function.
//----
template foo(T = int)
{
T foo();
}
void main()
{
foo(); //Calls foo!int.foo();
}
//----
I guess it's the "()" that saves our butts. Without it, we need explicit instantiation:
//----
template foo(T = int)
{
T foo();
}
void main()
{
pragma(msg, typeof(foo).stringof);
pragma(msg, typeof(foo!int).stringof);
}
//----
void
int()
//----
|
January 17, 2014 Re: default args for templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On 2014-01-17 14:44, Dicebot wrote: > I think it is impossible in general case: > > pragma(msg, StructSymbol.stringof); // "StructSymbol" or > "StructSymbol!(void)" ? > > Add aliases on top of it and it quickly gets messy. The compiler would first try the template symbol, if that fails try the instantiation. -- /Jacob Carlborg |
January 17, 2014 Re: default args for templates | ||||
---|---|---|---|---|
| ||||
Posted in reply to Manu | Manu:
> The context gives everything it needs.
I don't think so. If you pass one of those literals to a template by alias, you are passing the template name (the type constructor) or the instantiated template (the type)? Both are valid usages and I think the compiler can't choose by itself. So I think this is a bad idea.
Bye,
bearophile
|
Copyright © 1999-2021 by the D Language Foundation