Thread overview | ||||||
---|---|---|---|---|---|---|
|
January 31, 2007 IFTI with constant args, is this already possible? | ||||
---|---|---|---|---|
| ||||
So suppose I have a function template, one that is supposed to be instantiated via IFTI. Then, the user supplies an argument that is constant (ex: foo(42,"cat")). Is there any way that I can grab those constant arguments at compile time and use them to do compile time computation? Even better, can I do it in variadic templates? I'm envisioning a situation like this: // the string-to-integer conversion is done at compile time bignum bigAssInteger = "3420894398526094609987620490236001914309690234"; ... struct bignum { ulong[] data; ... void opAssign(char[] value)() { data = toBigInt!(value); } } or something like that. BigNums come to mind as a simple example, but I've run into this sort of thing other times too. Hard to put it down since I don't know if it's even possible, much less what it would look like. |
January 31, 2007 Re: IFTI with constant args, is this already possible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chad J | Chad J wrote: > So suppose I have a function template, one that is supposed to be instantiated via IFTI. Then, the user supplies an argument that is constant (ex: foo(42,"cat")). Is there any way that I can grab those constant arguments at compile time and use them to do compile time computation? Even better, can I do it in variadic templates? > > I'm envisioning a situation like this: > > // the string-to-integer conversion is done at compile time > bignum bigAssInteger = "3420894398526094609987620490236001914309690234"; There's no syntax sugar for this at present. (Though I've proposed it, and there are indications that it will happen). But: bigAssInteger = toBigInt!("3420894398526094609987620490236001914309690234"); is currently possible. Until we get early discard of templates, it's not really a good idea right now because compilation is slow while the compiler generates an enormous obj file full of junk. |
January 31, 2007 Re: IFTI with constant args, is this already possible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Don Clugston | Don Clugston wrote:
> Chad J wrote:
>
>> So suppose I have a function template, one that is supposed to be instantiated via IFTI. Then, the user supplies an argument that is constant (ex: foo(42,"cat")). Is there any way that I can grab those constant arguments at compile time and use them to do compile time computation? Even better, can I do it in variadic templates?
>>
>> I'm envisioning a situation like this:
>>
>> // the string-to-integer conversion is done at compile time
>> bignum bigAssInteger = "3420894398526094609987620490236001914309690234";
>
>
> There's no syntax sugar for this at present. (Though I've proposed it, and there are indications that it will happen).
> But:
>
> bigAssInteger = toBigInt!("3420894398526094609987620490236001914309690234");
>
> is currently possible.
>
> Until we get early discard of templates, it's not really a good idea right now because compilation is slow while the compiler generates an enormous obj file full of junk.
Ah. Thanks for the info.
|
February 01, 2007 Re: IFTI with constant args, is this already possible? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chad J | Chad J wrote:
> Don Clugston wrote:
>> Chad J wrote:
>>
>>> So suppose I have a function template, one that is supposed to be instantiated via IFTI. Then, the user supplies an argument that is constant (ex: foo(42,"cat")). Is there any way that I can grab those constant arguments at compile time and use them to do compile time computation? Even better, can I do it in variadic templates?
>>>
>>> I'm envisioning a situation like this:
>>>
>>> // the string-to-integer conversion is done at compile time
>>> bignum bigAssInteger = "3420894398526094609987620490236001914309690234";
>>
>>
>> There's no syntax sugar for this at present. (Though I've proposed it, and there are indications that it will happen).
>> But:
>>
>> bigAssInteger = toBigInt!("3420894398526094609987620490236001914309690234");
>>
>> is currently possible.
>>
>> Until we get early discard of templates, it's not really a good idea right now because compilation is slow while the compiler generates an enormous obj file full of junk.
>
> Ah. Thanks for the info.
BTW, my proposal was for something like:
BigInt opAssign(super T : char [])(T s){
static if (is(s == const)) {
// compile-time assign
setFromLiteral!(s);
} else {
// runtime-assign.
...
}
}
Requires (1) super in a template parameter list distinguishes between storage classes.
(2) is( A B == const) --> if A is a literal, sets B to be that literal.
Andrei's storageof() proposal could probably achieve the same thing.
|
Copyright © 1999-2021 by the D Language Foundation