New to D and mimicking C++ : how to implement std::integral_constant<>?
Basile B. via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Nov 7 22:36:08 PST 2016
On Monday, 7 November 2016 at 23:03:32 UTC, Picaud Vincent wrote:
> I need:
>
> 1/ a way to detect compile-time constant vs "dynamic" values
/**
* Indicates if something is a value known at compile time.
*
* Params:
* V = The value to test.
* T = Optional, the expected value type.
*/
template isCompileTimeValue(alias V, T...)
if (T.length == 0 || (T.length == 1 && is(T[0])))
{
enum isKnown = is(typeof((){enum v = V;}));
static if (!T.length)
enum isCompileTimeValue = isKnown;
else
enum isCompileTimeValue = isKnown && is(typeof(V) ==
T[0]);
}
///
unittest
{
string a;
enum b = "0";
enum c = 0;
static assert(!isCompileTimeValue!a);
static assert(isCompileTimeValue!b);
static assert(isCompileTimeValue!c);
static assert(isCompileTimeValue!(b,string));
static assert(isCompileTimeValue!(c,int));
static assert(!isCompileTimeValue!(c,char));
static assert(!isCompileTimeValue!(char));
}
More information about the Digitalmars-d-learn
mailing list