C++ guys hate static_if?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Mar 14 15:18:58 PDT 2013


On 3/14/13 6:02 PM, H. S. Teoh wrote:
> On Thu, Mar 14, 2013 at 05:54:05PM -0400, Andrei Alexandrescu wrote:
>> On 3/14/13 2:38 PM, Jonathan M Davis wrote:
>>> On Thursday, March 14, 2013 13:26:18 Andrei Alexandrescu wrote:
>>>> Walter has measured coverage of Phobos unittests a couple of times,
>>>> it's very high. But I agree it would be nice to have it as a target.
>>>
>>> Though this is exactly a case where 100% unit test coverage doesn't
>>> mean much.  All that means is that each path has been instantiated
>>> and run. It doesn't mean that it's covered enough possible
>>> instantiations to properly test the template.
>>
>> Concepts won't help there either.
> [...]
>
> It does help. For example, if the code wrongly assumes mutability for a
> particular template type, then it may work for most test cases (frankly,
> I find const/immutable unittest coverage in Phobos very poor) but fail
> when some daring user passes const(T) instead of T to the template. For
> example, you may have accidentally written the equivalent of:
>
> 	auto func(T)(T t) {
> 		Unqual!T u = t; //<-- spot the bug
> 		...
> 	}
>
> Under a concepts system, this would be caught early because the compiler
> would detect a concept mismatch (Unqual!T != T) when analysing the
> template code.
>
> Currently, though, if there is no unittest that tries instantiating the
> template with const(T), the bug goes undetected, because in all *tested*
> instantiations, Unqual!T == T.

Template constraints start from the most permissive end of the spectrum: 
by default there's no verification, and constraints add verification.

With typeclasses it's the other way around: by default nothing is 
allowed, so code needs to add permissions explicitly.

I agree that in that regard typeclasses are better than template 
constraints. Of course there are many other aspects to be considered 
when comparing the two.


Andrei


More information about the Digitalmars-d mailing list