Re: C++11 move constructors



On 26 June 2013 12:16, Murray Cumming <murrayc murrayc com> wrote:
On Wed, 2013-06-26 at 11:33 +0100, Reece Dunn wrote:


> In the server_without_bus example, the type is changed from
> `Glib::ustring` to `char *` in:
>
> -static Glib::ustring introspection_xml =
> +static auto introspection_xml =
>      "<node>"

I guess it should be const auto. But otherwise, the change seems good to
me. We don't actually use the ustring API on that.

Ok. Just checking.
 
> auto should only be used:
>
>   1/  if it is shorter (e.g. std::unordered_map<Glib::ustring,
> Glib::ustring>::const_iterator)
>
>   2/  if the deduced type matches the variable type
>
>
>
> e.g. declaring the `GPid` type `auto` does not buy you anything in
> terms of improved readability.

It avoids the person having to think about the actual type name, which
seems useful, though I guess it's a matter of taste. My opinion about
that changes day to day.

Is there any official discouragement from using it in this way?

Herb Sutter has some advice on auto usage (e.g. http://herbsutter.com/elements-of-modern-c-style/ and http://herbsutter.com/2013/06/13/gotw-93-solution-auto-variables-part-2/). There is also a discussion about auto usage on http://programmers.stackexchange.com/questions/180216/does-auto-make-c-code-harder-to-understand.

From what I can tell, the main issue is whether you want the actual type (e.g. std::shared_ptr<Derived>) or a different type (e.g. std::shared_ptr<Base>). This includes things like float vs double, and expressions like |int *p = NULL;| and |int *p = nullptr|. Aside from that it is a matter of style if you prefer auto over explicit types.

I personally use a mix of explicit types and auto, with auto being preferred for complex types.

- Reece


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]