Initializing defaults based on type.
Benjamin Thaut via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Mar 6 08:04:32 PST 2015
On Friday, 6 March 2015 at 15:36:47 UTC, anon wrote:
> Hi,
>
> I can't figure this out.
>
> struct Pair(T)
> {
> T x;
> T y;
>
> alias x c;
> alias y r;
> }
>
> What would like is that the x and y to be initialized to
> different values depending on type eg:
>
> struct Container
> {
> Pair!double sample1; // This will initialize sample1 with 0
> for both x and y
> Pair!int sample2; // This will initialize sample2 with 1
> for both x and y
> }
>
> currently I'm using two different struct one with doubles and
> the other with ints and initialized with default value but was
> wondering if its possible to do the above.
>
> anon
struct Pair(T)
{
static if(is(T == int))
enum int initValue = 1;
else
enum T initValue = 0;
T x = initValue;
T y = initValue;
alias x c;
alias y r;
}
More information about the Digitalmars-d-learn
mailing list