Thread overview | |||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 29, 2014 Typed variadic template syntax? | ||||
---|---|---|---|---|
| ||||
This is not an enhancement request, but it presents a potential enhancement. This is C++11 code: template<int... digits> struct value; template<> struct value<> { static const int v = 0; }; template<int first, int... rest> struct value<first, rest...> { static int const v = 10 * value<rest...>::v + first; }; You can translate it to D like this: enum isInt(T) = is(T == int); template value(xs...) if (allSatisfy!(isInt, xs)) { static if (xs.length == 0) enum value = 0; else enum value = xs[0] + 10 * value!(xs[1 .. $]); } This D code: template value(xs...) if (allSatisfy!(isInt, xs)) { Is slower to compile than this D code: template value(xs...) { So we could allow D code like: template value(int[] xs...) { I think it could be faster than using "if (allSatisfy!(isInt, xs))" and it's nicer looking. On the other hand I don't know how much common are template instantiations with values all of the same type (like all ints as in this case) in D code. Opinions welcome. Bye, bearophile |
January 29, 2014 Re: Typed variadic template syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Wednesday, 29 January 2014 at 00:14:31 UTC, bearophile wrote:
> On the other hand I don't know how much common are template instantiations with values all of the same type (like all ints as in this case) in D code.
>
> Opinions welcome.
>
> Bye,
> bearophile
Well, if you're going for a direct translation, that would be more like
template value(ints...) {
static if (!ints.length) enum value = 0;
else static if (!is(ints[0] == int)) static assert(false);
else enum value = ints[0] + 10*value!(ints[1..$]);
}
But that indeed illustrates a certain disconcert: we can already have variadic functions of the same type (void foo(int[] a...)). It would certainly be nice to have the same for template parameters.
|
January 29, 2014 Re: Typed variadic template syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 1/28/14 4:14 PM, bearophile wrote:
> This is not an enhancement request, but it presents a potential
> enhancement.
>
>
> This is C++11 code:
>
> template<int... digits> struct value;
>
> template<> struct value<> {
> static const int v = 0;
> };
>
> template<int first, int... rest> struct value<first, rest...> {
> static int const v = 10 * value<rest...>::v + first;
> };
>
>
>
> You can translate it to D like this:
>
>
> enum isInt(T) = is(T == int);
>
> template value(xs...) if (allSatisfy!(isInt, xs)) {
> static if (xs.length == 0)
> enum value = 0;
> else
> enum value = xs[0] + 10 * value!(xs[1 .. $]);
> }
int value(int xs[]...) {
int result = 0;
foreach (i; xs) {
result += 10 * result + i;
}
return result;
}
unittest
{
static assert(value(1, 2) == 21);
}
Andrei
|
January 29, 2014 Re: Typed variadic template syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Wednesday, 29 January 2014 at 07:02:00 UTC, Andrei Alexandrescu wrote:
> int value(int xs[]...) {
Ew! :)
|
January 29, 2014 Re: Typed variadic template syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On 01/29/2014 08:01 AM, Andrei Alexandrescu wrote: > > int value(int xs[]...) { > int result = 0; > foreach (i; xs) { > result += 10 * result + i; > } > return result; > } > > unittest > { > static assert(value(1, 2) == 21); > } > ... import std.range, std.algorithm; int value(int xs[]...) { return reduce!((a,b)=>10*a+b)(0,xs.retro); } unittest{ static assert(value()==0); static assert(value(1)==1); static assert(value(1,2)==21); static assert(value(1,2,3)==321); } |
January 29, 2014 Re: Typed variadic template syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Wednesday, 29 January 2014 at 09:50:13 UTC, Timon Gehr wrote:
> import std.range, std.algorithm;
> int value(int xs[]...) {
> return reduce!((a,b)=>10*a+b)(0,xs.retro);
> }
Sadly, you can't build a struct (with N int fields) or an enum this way.
|
January 29, 2014 Re: Typed variadic template syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | On 1/29/14 1:53 AM, Stanislav Blinov wrote:
> On Wednesday, 29 January 2014 at 09:50:13 UTC, Timon Gehr wrote:
>
>> import std.range, std.algorithm;
>> int value(int xs[]...) {
>> return reduce!((a,b)=>10*a+b)(0,xs.retro);
>> }
>
> Sadly, you can't build a struct (with N int fields) or an enum this way.
mixin
Andrei
|
January 29, 2014 Re: Typed variadic template syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrei Alexandrescu | On Wednesday, 29 January 2014 at 16:39:31 UTC, Andrei Alexandrescu wrote: > On 1/29/14 1:53 AM, Stanislav Blinov wrote: >> Sadly, you can't build a struct (with N int fields) or an enum this way. > > mixin m| |
January 29, 2014 Re: Typed variadic template syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | On Wednesday, 29 January 2014 at 09:53:15 UTC, Stanislav Blinov wrote:
> On Wednesday, 29 January 2014 at 09:50:13 UTC, Timon Gehr wrote:
>
>> import std.range, std.algorithm;
>> int value(int xs[]...) {
>> return reduce!((a,b)=>10*a+b)(0,xs.retro);
>> }
>
> Sadly, you can't build a struct (with N int fields) or an enum this way.
That was a problem for me today XD . Couldn't make a auto fct(string[] choices ...)(string[] id, string[] key ...) but I managed to find a workaround...
// KEYWORDS
enum USING = "USING.",
VAR = "VAR.",
LIST = "LIST.",
SET = "SET.",
ZSET = "ZSET.",
HASHMAP = "HASHMAP.",
LOCK = "LOCK.",
UNLOCK = "UNLOCK.",
SKIPCACHE = "SKIPCACHE.",
INCREMENT = "INCREMENT.",
DECREMENT = "DECREMENT.",
FRONT = "FRONT.",
BACK = "BACK.",
PUSH = "PUSH.",
POP = "POP.",
REPLACE = "REPLACE.",
TRIM = "TRIM.",
RETURNS = "RETURNS.",
LENGTH = "LENGTH.",
EXISTS = "EXISTS.",
VALUE = "VALUE.",
KEY = "KEY.",
STREAM = "STREAM.",
ERROR = "ERROR.",
void exec(string command)(){
foreach(str ; choice.splitter(".")){
writeln(str);
}
}
void main()
{
exec!(USING ~ VAR ~ ADD ~ USING ~ LIST ~ INSERT ~ LOCK)();
}
|
January 29, 2014 Re: Typed variadic template syntax? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Etienne | >
> void exec(string command)(){
> foreach(str ; choice.splitter(".")){
> writeln(str);
> }
> }
I'd like to take the opportunity to say how much I'd love to be able to do a static foreach rather than use recursion.
|
Copyright © 1999-2021 by the D Language Foundation