mardi 2 juin 2015

Is using auto as function return type good style? [duplicate]

This question already has an answer here:

Since C++11 I'm a fan of using auto and prefer it over manual type declarations. Auto deduces the right type, saves typing and forces variables to be initialised.

With C++14, we can also use auto as function return type. Now I'm a bit hesitant to use it in this area. It's useful for generic lambdas and templates with decltype, but isn't it bad style for plain simple functions like:

auto isOdd(int number)
{
  return number % 2 == 0;
}

And bad style in a class like:

class Person
{
public:
  Person(std::string name)
    : _name(name)
{ }

auto name() { return _name; }

private:
  std::string _name;
};

I've read Modern Effective C++ and I saw a presentation of Herb Sutter on using auto, but it doesn't say much about function return types.

Aucun commentaire:

Enregistrer un commentaire