Proposal: static template(fail)

Bill Baxter dnewsgroup at billbaxter.com
Fri Dec 14 11:31:33 PST 2007


Bill Baxter wrote:
> Robert DaSilva wrote:
>> Bruce Adams wrote:
>>> On Thu, 13 Dec 2007 20:53:23 -0000, Janice Caron
>>> <caron800 at googlemail.com> wrote:
>>>
>>>> On 12/13/07, Bill Baxter <dnewsgroup at billbaxter.com> wrote:
>>>>> If the template results in a something that's
>>>>> an ordinary syntax error then it's just a substitution failure.  
>>>>> "static
>>>>> assert" is special in that it short circuits that behavior.
>>>> Really? Wow! I'm going to have to try that now!
>>> So did it work?
>>> I guess you are using the Unix silence is golden principle.
>>
>> I've tried it and it didn't work for me.
> 
> Why be so cryptic?  Show us what you tried for goodness sake.
> 
> --bb

Ok so I tried it myself.  Apparently neither D nor C++ works the way I 
thought it did w.r.t SFINAE.  It seems that SFINAE only applies to 
things in the function's declaration, not its contents.  I suppose that 
does make sense, because otherwise it would be damned hard to figure out 
  when you have a syntax error in specialized templates.

Lutz Kettner has a pretty good explanation of how it works:
http://www.mpi-inf.mpg.de/~kettner/courses/lib_design_03/notes/meta.html
(Wikipedia's explanation is pathetic.)

My appologies.

Here are my tests:

=========C++=========
#include <stdio.h>

template <typename T>
struct Foo {
     T x;
     void spew() { printf("Generic struct"); }
};
template <typename T>
struct Foo<T*> {
     T x;
     void spew() {
         printf("Partial specialized struct\n");
         // T y = x.foo; // an error
     }

};


template <typename T>
void func(T x) {
     printf("generic func\n");
}

template <>
void func<int*>(int* x) {
     printf("total specialized func\n");
     //int z = x.foo; // an error
}

int main()
{
     int *x;
     func<int*>(x);

     Foo<int*> ptrFoo;
     ptrFoo.spew();

     return 0;
}


============D================

module sfinae;
import std.stdio;

void func(T)(T x) {
     writefln("generic version");
}

void func(T:T[])(T[] x) {
     writefln("specialized version");
     // writefln(T.foo); // an error
}

void main()
{
     int[] x;
     func!(int[])(x);
}
===============================


--bb



More information about the Digitalmars-d mailing list