[Issue 8640] Template struct/class member functions should compile conditionally

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Sep 11 09:45:22 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8640


Simen Kjaeraas <simen.kjaras at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras at gmail.com


--- Comment #3 from Simen Kjaeraas <simen.kjaras at gmail.com> 2012-09-11 09:45:47 PDT ---
(In reply to comment #2)
> However, as pointed out by bearophile, this approach means this:
> --------
> struct C(T) {
>     private T val;
>     @property void front()(T value) {
>         val = value;
>     }
> }
> void main() {
>     C!int ci;
>     auto f = &ci.front; //Should be &ci.front!()
>     assert(ci.val == 0);
>     f(1);
>     assert(ci.val == 1);
> }
> --------
> Ceases to work. So it does have client-side impacts :'(

Indeed. Now, there is another way to do it, which clearly marks the code as
'may or may not work, depending on template parameters', and that involves
static if.

If the method does not work with some types, the correct way to state that is
so that the compiler gives a sensible error message when someone tries to use
your code, not when your code tries to use something else.

In this case:

static if (__traits(compiles, (T value){val = value;})) {
    @property void front()(T value) {
        val = value;
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list