Parameter with indetermined tuple elements type?

Ali Çehreli acehreli at yahoo.com
Mon Jan 11 16:37:18 UTC 2021


On 1/11/21 7:27 AM, Marcone wrote:
> I want to create a function that receive a tuple (need be a tuple) with 
> indetermined length and indetermined elements type without template. The 
> argument need be a tuple, but length and elements types indetermineds. 
> How can I make it?

With isIntanceOf in a template constraint:

import std.traits;
import std.typecons;

void foo(T)(T t)
if (isInstanceOf!(Tuple, T))
{
}

unittest {
   static assert(__traits(compiles, foo(tuple(1, "hello"))));
   static assert(!__traits(compiles, foo(2)));
}

Or, you can put the check inside the body of the function to display a 
custom compilation error message:

void foo(T)(T t) {
   static assert (isInstanceOf!(Tuple, T),
                  "I can only work with a Tuple.");

}

But in that case, the compilation error points at the 'static assert' 
line, not where the function is called incorrectly from.

Ali


More information about the Digitalmars-d-learn mailing list