Templated Struct Constructor
Andrew Wiley
wiley.andrew.j at gmail.com
Sat Dec 10 10:21:35 PST 2011
On Sat, Dec 10, 2011 at 5:38 AM, Trass3r <un at known.com> wrote:
>> Looks like I can work around it by doing this:
>> ---
>> template makeTest(T, bool b) {
>> static if((b && something) || (!b && somethingElse)) {
>> alias Test makeTest;
>> }
>> else static assert(0, "some error message");
>> }
>> struct Test {
>> private:
>> this(T)(T info) {
>> }
>> }
>> void main() {
>> Test test = Test!(int, true)(5); // Error: template instance
>> Test!(int,true) Test is not a template declaration, it is a struct
>> }
>> ---
>>
>> But this solution isn't very satisfying because Test is no longer
>> directly protected by the static checks I want.
>
>
> Shouldn't this work too:
>
> struct Test(T, bool b)
>
> if((b && something ) || (!b && somethingElse))
> {
> this(T info)
> {
> }
> }
>
> It's still a bug imho though.
The problem is that I don't want to template the struct itself because
I want it to be a single type. That doesn't happen very often, but it
can happen when you're using Variant and such.
More information about the Digitalmars-d
mailing list