Templates class member functions not conditional?

monarch_dodra monarchdodra at gmail.com
Tue Sep 11 09:34:58 PDT 2012


On Tuesday, 11 September 2012 at 15:43:56 UTC, Jonathan M Davis 
wrote:
>
> Anything inside a template is not compiled unless the template 
> is
> instantiated, but if the template is instantiated, the entire 
> template is
> instantiated and compiled (save for static if branches which 
> aren't followed
> or inner templates which aren't instantiated). If C++ doesn't 
> work that way as
> well, I find that to be very bizarre. Regardless though, that's 
> how D works.
>
> - Jonathan M Davis

TY for putting up with me :D

C++ works a certain way, and I find it very bizarre that D 
doesn't work that way either. Then again, C++ doesn't have static 
if.



At the very least, I wish we could write:
--------
struct S(T)
{
     @property void front(T value)
         if(isAssignable!(T,T))
     {
         ...
     }
}
--------
That would make things less bulky and the intent much more 
streamlined with what we already have.

In this specific case, this doesn't work because "front" itself 
isn't a template, but given the encapsulating struct *is* a 
template, I think it should be made to work.

As a matter of fact, I don't see why we can't use conditional 
implementations ALL the time? Like: If some enum is defined, or 
if a certain type works or whatever.


More information about the Digitalmars-d-learn mailing list