www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - IFTI with constant args, is this already possible?

reply Chad J <gamerChad _spamIsBad_gmail.com> writes:
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.
Jan 30 2007
parent reply Don Clugston <dac nospam.com.au> writes:
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.
Jan 30 2007
parent reply Chad J <gamerChad _spamIsBad_gmail.com> writes:
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.
Jan 31 2007
parent Don Clugston <dac nospam.com.au> writes:
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.
Feb 01 2007