Comment #0 by andrej.mitrovich — 2012-09-13T20:33:42Z
import std.typecons;
import std.bitmanip;
static import core.stdc.config;
alias Typedef!(core.stdc.config.c_ulong) c_ulong;
struct Foo
{
mixin(bitfields!(
c_ulong, "NameOffset", 31,
c_ulong, "NameIsString", 1
));
}
void main()
{ }
It's great to see we've deprecated typedef for a library solution that doesn't work.
:\DMD\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d(77): Error: Cannot interpret Typedef!(uint,0u) at compile time
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d(77): Error: expression (Typedef!(uint,0u)).opDispatch() < 0u is not constant or does not evaluate to a bool
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d(111): Error: Cannot interpret Typedef!(uint,0u) at compile time
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d(159): Error: template instance std.bitmanip.createAccessors!("_NameOffset_NameIsString",Typedef!(uint,0u),"NameOffset",31,0u) error instantiating
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d(213): instantiated from here: createFields!("_NameOffset_NameIsString",0,Typedef!(uint,0u),"NameOffset",31,Typedef!(uint,0u),"NameIsString",1)
test.d(11): instantiated from here: bitfields!(Typedef!(uint,0u),"NameOffset",31,Typedef!(uint,0u),"NameIsString",1)
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d(77): Error: Cannot interpret Typedef!(uint,0u) at compile time
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d(77): Error: expression (Typedef!(uint,0u)).opDispatch() < 0u is not constant or does not evaluate to a bool
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d(111): Error: Cannot interpret Typedef!(uint,0u) at compile time
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d(159): Error: template instance std.bitmanip.createAccessors!("_NameOffset_NameIsString",Typedef!(uint,0u),"NameIsString",1,31u) error instantiating
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d(160): instantiated from here: createFields!("_NameOffset_NameIsString",31u,Typedef!(uint,0u),"NameIsString",1)
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d(213): instantiated from here: createFields!("_NameOffset_NameIsString",0,Typedef!(uint,0u),"NameOffset",31,Typedef!(uint,0u),"NameIsString",1)
test.d(11): instantiated from here: bitfields!(Typedef!(uint,0u),"NameOffset",31,Typedef!(uint,0u),"NameIsString",1)
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d(160): Error: template instance std.bitmanip.createFields!("_NameOffset_NameIsString",31u,Typedef!(uint,0u),"NameIsString",1) error instantiating
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d(213): instantiated from here: createFields!("_NameOffset_NameIsString",0,Typedef!(uint,0u),"NameOffset",31,Typedef!(uint,0u),"NameIsString",1)
test.d(11): instantiated from here: bitfields!(Typedef!(uint,0u),"NameOffset",31,Typedef!(uint,0u),"NameIsString",1)
D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\bitmanip.d(213): Error: template instance std.bitmanip.createFields!("_NameOffset_NameIsString",0,Typedef!(uint,0u),"NameOffset",31,Typedef!(uint,0u),"NameIsString",1) error instantiating
test.d(11): instantiated from here: bitfields!(Typedef!(uint,0u),"NameOffset",31,Typedef!(uint,0u),"NameIsString",1)
test.d(11): Error: template instance std.bitmanip.bitfields!(Typedef!(uint,0u),"NameOffset",31,Typedef!(uint,0u),"NameIsString",1) error instantiating
Comment #1 by andrej.mitrovich — 2012-10-02T16:04:02Z
(In reply to comment #0)
> import std.typecons;
> import std.bitmanip;
> static import core.stdc.config;
>
> alias Typedef!(core.stdc.config.c_ulong) c_ulong;
>
> struct Foo
> {
> mixin(bitfields!(
> c_ulong, "NameOffset", 31,
> c_ulong, "NameIsString", 1
> ));
> }
>
> void main()
> { }
This is a problem with mixin template Proxy(alias a). It uses a template dispatch but doesn't take into account type properties (min, max, init..). Although this can be fixed another issue pops up:
Error: e2ir: cannot cast result of type uint to type Typedef!(int,0)
Here's the hackish temporary workaround of Proxy.opDispatch:
template opDispatch(string name)
{
static if (canFind(["min", "max", "init", "sizeof", "nan", "mangleof", "stringof", "alignof", "infinity", "dig", "epsilon", "mant_dig", "max_10_exp", "max_exp", "min_10_exp", "min_exp", "min_normal", "re", "im", "classinfo"], name))
{
mixin("enum opDispatch = typeof(a)." ~ name ~ ";");
}
else
static if (is(typeof(__traits(getMember, a, name)) == function))
{
// non template function
auto ref opDispatch(this X, Args...)(Args args) { return mixin("a."~name~"(args)"); }
}
else static if (is(typeof(mixin("a."~name))) || __traits(getOverloads, a, name).length != 0)
{
// field or property function
@property auto ref opDispatch(this X)() { return mixin("a."~name); }
@property auto ref opDispatch(this X, V)(auto ref V v) { return mixin("a."~name~" = v"); }
}
else
{
// member template
template opDispatch(T...)
{
auto ref opDispatch(this X, Args...)(Args args){ return mixin("a."~name~"!T(args)"); }
}
}
}