Mini-proposal of class name omission in constructor?

175 views
Skip to first unread message

Andrew Tomazos

unread,
Apr 12, 2019, 6:29:56 AM4/12/19
to std-pr...@isocpp.org
Each constructor declaration within a class definition of C requires the repetition of the (injected) class name C.  I suspect this is redundant.  Couldn't we allow the C to be taken as implied, and permit it to be omitted?  Likewise for destructor?

// TODAY
class C {
    C();
    C(int);
    ~C();
};

// PROPOSED
class C {
    ();
    (int);
    ~();
};

The motivation is simply to get rid of some DRY violations.

Bo Persson

unread,
Apr 12, 2019, 6:55:05 AM4/12/19
to std-pr...@isocpp.org
That's not a DRY violation. C(int); means that you didn't intend to
write f(int);, so it is not really redundant.


Bo Persson



Andrew Tomazos

unread,
Apr 12, 2019, 6:56:43 AM4/12/19
to std-pr...@isocpp.org
Why can't (int); also mean that you didn't intend to write f(int); ?
 

Domen Vrankar

unread,
Apr 12, 2019, 8:09:13 AM4/12/19
to std-pr...@isocpp.org
It could but there is also the issue of harder spotting it/searching for constructors for those of us that use text editors/diff tools instead of full blown IDEs.

Regards,
Domen
Message has been deleted
Message has been deleted

Magnus Fromreide

unread,
Apr 14, 2019, 2:58:10 PM4/14/19
to std-pr...@isocpp.org
One advantage(?) of this proposal is that it allows constructors for anonymous
classes.

class {
() : member() { /* something */ }
~() { /* something* / }
private:
Foo member;
} variable;

/MF

> Regards,
> Domen
>
> >
>
> --
> You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
> To post to this group, send email to std-pr...@isocpp.org.
> To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAKgx6BKAipbuQo4nEiTJDLspDx3tBn%2B15pmOoFE_gi4t%2BZakyw%40mail.gmail.com.

Domen Vrankar

unread,
Apr 15, 2019, 2:14:47 AM4/15/19
to std-pr...@isocpp.org
I personaly don't see that as such an advantage that would be a compelling reason to change the core language.
Lambda functions are already a terse syntax for when you don't require a destructor.
And in combination with p0052 scope guards you also get the destructor part - and such a templated class can allready be writen now in ~10 lines of code, put inside a util header and then used everywhere you like.

Regards,
Domen

Jake Arkinstall

unread,
Apr 15, 2019, 3:49:36 AM4/15/19
to std-pr...@isocpp.org
I'm okay with the idea of a generic constructor (though I'm far from strongly opinioned either way) . I don't like the idea of leaving it blank though. Python's __init__ and PHP's __construct are clear, and that's an approach I'd rather. Internally, of course, it would just be relabelled with the class name.


--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.

kylekn...@gmail.com

unread,
Apr 15, 2019, 9:34:26 AM4/15/19
to ISO C++ Standard - Future Proposals
Yes, there are many different ways languages handle this (I actually like D's "this(int)" construct).  I agree with Andrew and Jake that something would be desirable that makes the specification of special functions simpler, but "removing" the typename is prone to confusion.  I also suspect that with metaclasses coming down the road, this problem will become less of an issue to deal with.

To unsubscribe from this group and stop receiving emails from it, send an email to std-pr...@isocpp.org.
Reply all
Reply to author
Forward
0 new messages