Proposal: static template(fail)

Rioshin an'Harthen rharth75 at hotmail.com
Thu Dec 13 11:11:41 PST 2007


"Ary Borenszweig" <ary at esperanto.org.ar> kirjoitti viestissä 
news:fjrqqp$1f0e$1 at digitalmars.com...
> Janice Caron wrote:
>> On 12/13/07, Jason House <jason.james.house at gmail.com> wrote:
>>> Janice Caron Wrote:
>>>
>>>> On 12/13/07, Aziz K. <aziz.kerim at gmail.com> wrote:
>>>>> Why not use a static assert: static assert(0, "Substitution Failure");
>>>> A static assert is a compile error. Different thing. What you want is
>>>> for the template not to be instanted, not for it to be instantiated
>>>> with a compile error.
>>>>
>>>> Put it another way. Suppose you write
>>>>
>>>>     S(int) s;
>>>>
>>>> Which would you prefer - an error message saying that s couldn't be
>>>> instantiated (giving the filename and line number of the above line),
>>>> or an error message saying that a static assert in the middle of some
>>>> library file had been hit?
>>> I'd prefer a static assert.
>>> As with all errors, I'd want a backtrace.
>>
>> You're missing the point, which is IT'S NOT AN ERROR. It's a
>> substitution failure - and substitution failure IS NOT AN ERROR.
>> That's a basic principle of template programming.
>
> I don't understand how it's not an error if you can't instantiate the 
> template.

It should *not* be an error of the template that a certain call can't 
instantiate it, since another (overloaded) template, as Gregor mentioned, 
can possibly be instantiated. That's basically what the current template 
with static if forces us to do: make it an error in the template, if we want 
to avoid template bloat. Instead, the error should be at the site of 
instatiation, if no template matches.

When the template instatiation fails because we've found a matching template 
where specific overloaded specializations are combined into the single 
template, and where a specific condition doesn't exist for the template to 
be useful - eg. if it fails the isFloatingPoint!(T) and 
passesSomeVeryComplicatedTest!(T) example conditions from Janice's first 
post in this thread, then

1) with a static assert and no specializations we get an error, inside the 
code of the template;
2) with Janice's syntax, the compiler would note that this template didn't 
match, and proceed onward with the testing of other templates, finally 
giving an error at the location of instantiation.




More information about the Digitalmars-d mailing list