-
it'd be great to add more reasoning on discouraging function returns// Classic C++ function declaration
std::vector<int> foo_func1()
{
// ...
}
// Modern C++ style
auto foo_func2() -> std::vector<int>
{
// ...
} advantages listed in https://stackoverflow.com/a/11215414/13503626:
struct my_awesome_type {
typedef std::vector<int> integer_sequence;
integer_sequence get_integers() const;
};
// Classic C++ style
my_awesome_type::integer_sequence my_awesome_type::get_integers() const {
// ...
}
// Modern C++ style
auto my_awesome_type::get_integers() const -> integer_sequence {
// ...
} variable declarationvariable names aligned left and types on right gives better readability. // Classic C++ declaration order // Modern C++ style
const char* s = "Hello"; auto s = "Hello";
widget w = get_widget(); auto w = get_widget();
employee e{ empid }; auto e = employee{ empid };
widget w{ 12, 34 }; auto w = widget{ 12, 34 }; this gets more prominent as types get verbose: // Classic C++ declaration order
std::vector<std::vector<int>> nums_1 = {{1, 2}, {3, 4}};
std::array<std::pair<std::string, std::string>, 2> pairs_1 = {{{"a", "1"}, {"b", "2"}}};
// Modern C++ style
auto nums_2 = std::vector<std::vector<int>>{{1, 2}, {3, 4}};
auto pairs_2 = std::array<std::pair<std::string, std::string>, 2>{{{"a", "1"}, {"b", "2"}}}; in above case, the name Additional Contextagainst
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Beta Was this translation helpful? Give feedback.
While I agree with most of these points in favor of auto in general (Rust uses similar style, where names go before types, and it's quite convenient), I'm not sure whether they'd be viable for BN.
That's the first time I'm seeing such "modern" style tbh. Everyone is familiar with the classic style, almost every codebase out there uses it, all our dependencies use it, and I highly doubt we have any usages of the "modern" style either.
Using auto here would just go against the unspoken established style, and further confuse many contributors who don't have a strong grasp on modern C++ features.
Alternative solution here is just to separate return type and function name by…