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