Templates class member functions not conditional?

monarch_dodra monarchdodra at gmail.com
Tue Sep 11 06:07:08 PDT 2012


On Tuesday, 11 September 2012 at 11:33:05 UTC, bearophile wrote:
> This doesn't look bad, just remember this doesn't work:
>
>
> struct C(T) {
>     private T val;
>     @property void front()(T value) {
>         val = value;
>     }
> }
> void main() {
>     C!int ci;
>     auto f = &ci.front;
>     assert(ci.val == 0);
>     f(1);
>     assert(ci.val == 1);
> }
>
> Bye,
> bearophile

True, but this does: auto f = &ci.front!();

That said, it looks horrible, and, client code should not be 
affected in such a way. So yeah, not a great solution. Plus, it 
creates a new semantic which is not very obvious.

I'll have to admit, I'm really not a fan of of such explicit 
conditional implementations. It really burdens the code, and 
forces the implementer to think about the required conditions to 
use the function, rather than let the compiler issue a failure 
when it just *does* happen fail.

I am opening an enhancement request for this.


More information about the Digitalmars-d-learn mailing list