Sintax suggar for empty constructors

87 views
Skip to first unread message

pau.mo...@gmail.com

unread,
Sep 20, 2017, 4:36:27 AM9/20/17
to ISO C++ Standard - Future Proposals
Hello,

I would like to propose a simple syntax suggar for empty constructors. Is my first proposal so is very possible that I'm doing the proposal in the wrong way, please feel free to make any necessary correction. Also, I aplogize in advance for my poor english skills.

There's some simple objects with few members which are initialized in the constructor member initializer list but there's no code to write in constructor's body:

template <typename T>
struct point3d {
    T x
{}, y{}, z{};
    point3d
() = default;
    point3d
(T xx, T yy, T zz) : x{xx}, y{yy}, z{zz} {}
//                     empty constructor body ----> ^^
};

struct ip {
    std
::uint32_t address{0x7f000001};

    ip
(std::uint32_t A, std::uint32_t B, std::uint32_t C, std::uint32_t D) :
        address
{D + (C << 8u) + (B << 16u) + (A << 24u)}
   
{}
//  ^^ <---- empty constructor body
    ip
(std::uint32_t value) :
        address
{value}
   
{}
//  ^^ <---- empty constructor body
};

I would like to propose to avoid constructor body if it is going to be empty:

template <typename T>
struct point3d {
    T x
{}, y{}, z{};
    point3d
() = default;
    point3d
(T xx, T yy, T zz) : x{xx}, y{yy}, z{zz};
//                       no constructor body ----> ^
};

struct ip {
    std
::uint32_t address{0x7f000001};
    ip
() = default;
    ip
(std::uint32_t A, std::uint32_t B, std::uint32_t C, std::uint32_t D) :
        address
{D + (C << 8u) + (B << 16u) + (A << 24u)};
//                            no constructor body ----> ^
    ip
(std::uint32_t value) :
        address
{value};
//                    ^ <---- no constructor body
};

An alternative could be to avoid the semicolon at the end:

template <typename T>
struct point3d {
    T x
{}, y{}, z{};
    point3d
() = default;
    point3d
(T xx, T yy, T zz) : x{xx}, y{yy}, z{zz}
//                              ^^^^^^^^^^^^^^^^^^^ <---- no constructor body
};

struct ip {
    std
::uint32_t address{0x7f000001};
    ip
() = default;
    ip
(std::uint32_t A, std::uint32_t B, std::uint32_t C, std::uint32_t D) :
        address
{D + (C << 8u) + (B << 16u) + (A << 24u)}
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ <---- no constructor body
    ip
(std::uint32_t value) :
        address
{value}
//      ^^^^^^^^^^^^^^ <---- no constructor body
};

But I think that is not a good idea because it can result in hard to read code. This omission of constructor body shall be used only in -obviously- definition, and wouldn't have any effect in declaration:

template <typename T>
struct point3d {
    T x
{}, y{}, z{};
    point3d
(); // Ctor declaration.
    point3d
(T xx, T yy, T zz); // Ctor declaration
};

point3d
::point3d(); // Ctor definition
//                ^ <---- no constructor body

point3d
::point3d() : x{xx}, y{yy}, z{zz}; // Ctor definition
//            no constructor body ----> ^

struct ip {
    std
::uint32_t address{0x7f000001};
    ip
(); // Ctor declaration
    ip
(std::uint32_t A, std::uint32_t B, std::uint32_t C, std::uint32_t D); // Ctor declaration
    ip
(std::uint32_t value); // Ctor declaration
};

ip
::ip(); // Ctor definition
//      ^ <---- no constructor body

ip
::ip() : address{D + (C << 8u) + (B << 16u) + (A << 24u)};
//                               no constructor body ----> ^
ip
::ip() : address{value};
//                       ^ <---- no constructor body




Omitting_empty_ctor_bodies.pdf

Justin Bassett

unread,
Sep 20, 2017, 4:55:32 AM9/20/17
to std-pr...@isocpp.org
I don't see how this adds any value. `{}` is 2 characters. There are practically no savings in this idea (1 char using the preferred semicolon).

--
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-proposals+unsubscribe@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/1244e43e-e75a-41f5-9aa9-68e7abab202f%40isocpp.org.
Reply all
Reply to author
Forward
0 new messages