On 22.03.2020 05:10, Paavo Helde wrote:
> On 22.03.2020 1:17, JiiPee wrote:
>> Is it a good idea to use auto -return value with all class member
>> function, or use the return type?
>>
>> this:
>>
>> class Render
>>
>> int getScreenWidth() const;
>> int getScreenHeight() const;
>>
>> const std::vector<std::vector<T>>& getScreen();
>>
>>
>> or this:
>>
>> class Render
>>
>> auto getScreenWidth() const;
>> auto getScreenHeight() const;
>>
>> const auto& getScreen();
>>
>> ?
>>
>> Or is some kind of mix best solution (like using auto only with
>> long/difficult return types)?
>
> This depends first on the scope and lifetime of the project, there are
> no universal rules. In small and short-living projects one can obviously
> use much more 'auto' than in an official interface meant to fix a
> library public interface for decades.
>
> Anyway, auto should be used only if the return type is obvious.
When even experienced programmers use "auto" to refer to inferred return
type, then maybe it's time to give up the terminology struggle.
So, let's agree that henceforth "auto" means inferred return type.
We can use the term "modern syntax" for trailing return type. Or just
spell it out, "trailing return type".
> With
> things like getScreenWidth() it's not so obvious, there are some people
> who believe widths and heights should be unsigned.
Well, Mr. Flibble is in that group, as I recall. I think it's a kind of
group affinity madness akin to religion. Happily the leading voices of
the C++ community have now for some years argued against that idiocy,
and the core guidelines recommend against it, <url:
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-mix>.
> So in order to use
> the result in calculations, and to avoid various compilers' warnings
> about signed-unsigned mix, one really needs to know if the return type
> is signed or unsigned. The function declaration is the best place to
> specify this, for both the users of the function and for the potential
> future code maintainers.
Yep.
> Besides, 'auto' is more characters to type than 'int'.
>
> With more complex types like in getScreen() the things are not so clear
> any more, too much noise on the screen is no good either. If the
> function itself is really trivial "return member_;" then I guess using
> 'auto' would be fine or even preferred.
>
> For anything more complex than a trivial getter I would do without auto
> and use some typedefs or aliases to reduce the clutter instead.
- Alf