why auto i = same_const_variable could not deduce “const”?

前端 未结 2 1930
北荒
北荒 2021-01-06 10:52
const int ci = 10;
auto i = ci;  // i will be \"int\" instead of \"const int\"
i = 20;

I am wondering why auto is designed for this kind of behavio

2条回答
  •  情深已故
    2021-01-06 11:12

    auto by itself means that you want a new, locally-owned variable with a copy of the given value. const-ness is not part of value. An int is an int whether it's specified using a literal, a named constant, an expression, or a non-const variable.

    auto i = 3,
         j = i,
         k = ci,
         m = 3 + 4; // All these variables are type int.
    

    To get a constant of deduced type, you can still use auto const. This expresses within the declaration how the variable may be used.

    const auto i = 3;
    

    Since C++14, there is also the decltype(auto) specifier which applies decltype to the initializer, to make a carbon copy of the given variable. Perhaps that's really what you expected:

    decltype(auto) i = ci; // i receives type const int.
    

    Live demo.

    decltype(auto) is a bit tricky, though, and it has few use cases aside from its original purpose relating to deciding the return type of function call wrappers. Unless there's a good reason, choose const auto or const int instead.

    Another alternative is to use a forwarding reference, spelled auto &&. This refers to the variable or value that initializes it, whatever that may be.

    auto && i = ci; // i receives type const int & and aliases ci.
    

    This is less expressive and specific, but reliably declares i as an alias to ci. The other thing you tried was auto &, which is similar but only allows forming a reference to a preexisting variable.

    auto & i = ci; // i receives type const int & and aliases ci.
    

    A reference to a const int variable must be of type const int &, because otherwise it would permit illegal modification.

提交回复
热议问题