Please take a look at the following simple code:
class Foo
{
public:
Foo(){}
~Foo(){}
Foo(const Foo&){}
Foo& operator=(const Foo&) { return *this; }
};
static Foo g_temp;
const Foo& GetFoo() { return g_temp; }
I tried to use auto like this:
auto my_foo = GetFoo();
I expected that my_foo will be a constant reference to Foo, which is the return type of the function. However, the type of auto is Foo, not the reference. Furthermore, my_foo is created by copying g_temp. This behavior isn't that obvious to me.
In order to get the reference to Foo, I needed to write like this:
const auto& my_foo2 = GetFoo();
auto& my_foo3 = GetFoo();
Question: Why does auto deduce the return type of GetFoo as an object, not a reference?
Read this article: Appearing and Disappearing consts in C++
Type deduction for auto variables in C++0x is essentially the same as
for template parameters. (As far as I know, the only difference
between the two is that the type of auto variables may be deduced from
initializer lists, while the types of template parameters may not be.)
Each of the following declarations therefore declare variables of type
int (never const int):
auto a1 = i;
auto a2 = ci;
auto a3 = *pci;
auto a4 = pcs->i;
During type deduction for template parameters and auto variables, only
top-level consts are removed. Given a function template taking a
pointer or reference parameter, the constness of whatever is pointed
or referred to is retained:
template<typename T>
void f(T& p);
int i;
const int ci = 0;
const int *pci = &i;
f(i); // as before, calls f<int>, i.e., T is int
f(ci); // now calls f<const int>, i.e., T is const int
f(*pci); // also calls f<const int>, i.e., T is const int
This behavior is old news, applying as it does to both C++98 and
C++03. The corresponding behavior for auto variables is, of course,
new to C++0x:
auto& a1 = i; // a1 is of type int&
auto& a2 = ci; // a2 is of type const int&
auto& a3 = *pci; // a3 is also of type const int&
auto& a4 = pcs->i; // a4 is of type const int&, too
Since you can retain the cv-qualifier if the type is a reference or pointer, you can do:
auto& my_foo2 = GetFoo();
Instead of having to specify it as const (same goes for volatile).
Edit: As for why auto deduces the return type of GetFoo() as a value instead of a reference (which was your main question, sorry), consider this:
const Foo my_foo = GetFoo();
The above will create a copy, since my_foo is a value. If auto were to return an lvalue reference, the above wouldn't be possible.
You can take simple answer as granted by MSVC Technical documentation:
Using auto drops references, const qualifiers, and volatile
qualifiers.
You don't need a function to achieve similar results. Consider:
int var;
const int & cref = var;
auto avar = cref;
The type of avar will be int not const int & nor int &.
To go a bit deeper into this we can go to cppreference.com and refer to template argument deduction analogy.
For example, given const auto& i = expr;, the type of i is exactly the type of the argument u in an imaginary template template<class U> void f(const U& u).
We tend to intuitively use templates and until it works we never really bother about template argument deduction rules. These rules are in fact quite complex. But we can make the "imaginary template for auto" non-imaginary and test what would have happened if we provided auto i = expr; to the template. In our case the template may look like this:
template<class U>
void f(U u)
{
cout << type_name<decltype(u)>() << "\n";
}
Let's feed it with variables with types of our interest:
int i = 0;
int & ir = i;
const int & cir = i;
f(i);
f(ir);
f(cir);
And the what's at the output?
int
int
int
The same thing would have happened with auto as predicted by MSVC docs :)
Here's the code if you wanted to play.
Here's also example from MS website if it disappeared one day
// cl.exe /analyze /EHsc /W4
#include <iostream>
using namespace std;
int main( )
{
int count = 10;
int& countRef = count;
auto myAuto = countRef;
countRef = 11;
cout << count << " ";
myAuto = 12;
cout << count << endl;
}
In the previous example, myAuto is an int, not an int reference, so the output is 11 11, not 11 12 as would be the case if the reference qualifier hadn't been dropped by auto
Related
Trying to understand working of structured bindings with const and references in particular as std::tuple is decomposed into named variables.
In the following case, it makes sense that a would be of type const int, with int& for b since int& const == int&, but how come the type of a1 isn't const int&? Is & only applied to the return object of get()?
int x;
std::tuple<int, int&> get()
{
return std::tuple<int, int&>(9, x);
}
int main()
{
const auto [a, b] = get();
const auto& [a1, b1] = get();
static_assert(std::is_same_v<const int, decltype(a)>);
static_assert(std::is_same_v<int&, decltype(b)>);
static_assert(std::is_same_v<const int, decltype(a1)>);
static_assert(std::is_same_v<int&, decltype(b1)>);
}
According to cpp-insights, that's how unpacking works. It's clear how it's const int& a1. however static_assert claims otherwise. Why the conflict? How else would the return from get() be decomposed?
const std::tuple<int, int &> __get12 = get();
const int && a = std::get<0UL>(static_cast<const std::tuple<int, int &> &&>(__get12));
int & b = std::get<1UL>(static_cast<const std::tuple<int, int &> &&>(__get12));
const std::tuple<int, int &> & __get13 = get();
const int & a1 = std::get<0UL>(__get13);
int & b1 = std::get<1UL>(__get13);
In simple terms, that's how I imagined would happen but it doesn't seem like it:
const auto& t = get();
const int& a1 = std::get<0>(t);
int& b1= std::get<1>(t);
EDIT:
The following works then that means structured binding does indeed not discard references, and perhaps it's just what decltype does as it returns the type of the element only not including the reference?
std::tuple<int> tx = std::make_tuple(42);
auto& [xz] = tx;
decltype(xz) yz = 0; // int& yz = 0;
static_assert(std::is_same_v<int, decltype(yz)>);
xz = 31; // tx<0> => 31
Structured bindings are not regular variables and are treated differently by decltype. The output of cppinsights is only a close approximation. cppreference says:
decltype(x), where x denotes a structured binding, names the referenced type of that structured binding. In the tuple-like case, this is the type returned by std::tuple_element, which may not be a reference even though a hidden reference is always introduced in this case. This effectively emulates the behavior of binding to a struct whose non-static data members have the types returned by tuple_element, with the referenceness of the binding itself being a mere implementation detail.
The reference qualifier (&) does apply to the intermediary object (named __get31 in your question).
Conceptually, it helps to compare with a struct used in this way:
struct tup {
int a;
int& b;
};
tup& value = get();
In that case, even though decltype(value) is tup &, decltype(value.a) is int, just like the structured binding a1 of a std::tuple<int, int&> & has a decltype of int.
Please take a look at the following simple code:
class Foo
{
public:
Foo(){}
~Foo(){}
Foo(const Foo&){}
Foo& operator=(const Foo&) { return *this; }
};
static Foo g_temp;
const Foo& GetFoo() { return g_temp; }
I tried to use auto like this:
auto my_foo = GetFoo();
I expected that my_foo will be a constant reference to Foo, which is the return type of the function. However, the type of auto is Foo, not the reference. Furthermore, my_foo is created by copying g_temp. This behavior isn't that obvious to me.
In order to get the reference to Foo, I needed to write like this:
const auto& my_foo2 = GetFoo();
auto& my_foo3 = GetFoo();
Question: Why does auto deduce the return type of GetFoo as an object, not a reference?
Read this article: Appearing and Disappearing consts in C++
Type deduction for auto variables in C++0x is essentially the same as
for template parameters. (As far as I know, the only difference
between the two is that the type of auto variables may be deduced from
initializer lists, while the types of template parameters may not be.)
Each of the following declarations therefore declare variables of type
int (never const int):
auto a1 = i;
auto a2 = ci;
auto a3 = *pci;
auto a4 = pcs->i;
During type deduction for template parameters and auto variables, only
top-level consts are removed. Given a function template taking a
pointer or reference parameter, the constness of whatever is pointed
or referred to is retained:
template<typename T>
void f(T& p);
int i;
const int ci = 0;
const int *pci = &i;
f(i); // as before, calls f<int>, i.e., T is int
f(ci); // now calls f<const int>, i.e., T is const int
f(*pci); // also calls f<const int>, i.e., T is const int
This behavior is old news, applying as it does to both C++98 and
C++03. The corresponding behavior for auto variables is, of course,
new to C++0x:
auto& a1 = i; // a1 is of type int&
auto& a2 = ci; // a2 is of type const int&
auto& a3 = *pci; // a3 is also of type const int&
auto& a4 = pcs->i; // a4 is of type const int&, too
Since you can retain the cv-qualifier if the type is a reference or pointer, you can do:
auto& my_foo2 = GetFoo();
Instead of having to specify it as const (same goes for volatile).
Edit: As for why auto deduces the return type of GetFoo() as a value instead of a reference (which was your main question, sorry), consider this:
const Foo my_foo = GetFoo();
The above will create a copy, since my_foo is a value. If auto were to return an lvalue reference, the above wouldn't be possible.
You can take simple answer as granted by MSVC Technical documentation:
Using auto drops references, const qualifiers, and volatile
qualifiers.
You don't need a function to achieve similar results. Consider:
int var;
const int & cref = var;
auto avar = cref;
The type of avar will be int not const int & nor int &.
To go a bit deeper into this we can go to cppreference.com and refer to template argument deduction analogy.
For example, given const auto& i = expr;, the type of i is exactly the type of the argument u in an imaginary template template<class U> void f(const U& u).
We tend to intuitively use templates and until it works we never really bother about template argument deduction rules. These rules are in fact quite complex. But we can make the "imaginary template for auto" non-imaginary and test what would have happened if we provided auto i = expr; to the template. In our case the template may look like this:
template<class U>
void f(U u)
{
cout << type_name<decltype(u)>() << "\n";
}
Let's feed it with variables with types of our interest:
int i = 0;
int & ir = i;
const int & cir = i;
f(i);
f(ir);
f(cir);
And the what's at the output?
int
int
int
The same thing would have happened with auto as predicted by MSVC docs :)
Here's the code if you wanted to play.
Here's also example from MS website if it disappeared one day
// cl.exe /analyze /EHsc /W4
#include <iostream>
using namespace std;
int main( )
{
int count = 10;
int& countRef = count;
auto myAuto = countRef;
countRef = 11;
cout << count << " ";
myAuto = 12;
cout << count << endl;
}
In the previous example, myAuto is an int, not an int reference, so the output is 11 11, not 11 12 as would be the case if the reference qualifier hadn't been dropped by auto
// case 1
const int i = 42;
const auto &k = i;
// case 2
const int i = 42;
auto &k = i;
Do we need the const keyword before auto in this scenario? After all, a reference (k) to an auto-deduced type will include the top level const of the object (const int i). So I believe k will be a reference to an integer that is constant (const int &k) in both cases.
If that is true, does that mean that const auto &k = i; in case 1 is replaced by the compiler as just const int &k = i; (auto being replaced with int)? Whereas in case 2, auto is replaced with const int?
auto keyword automatically decides the type of the variable at compile time.
In your first case, auto is reduced to int where it's reduced to const int in the second case. So, both of your cases are reduced to the same code as:
const int &k = i;
However, it's better to have the const explicitly for better readability and to make sure your variable TRULY is const.
Type deduction with auto works like template argument type deduction with a few exceptions that don't apply in the given example. Hence
const int i = 42;
const auto& k1 = i; // same as const int& k1 = i;
auto& k2 = i; // same as (const int)& k2 = i;
It is probably more readable to add the const qualifier nevertheless.
Here is another example where favoring brevity with auto is misleading:
int *i;
const auto k1 = i; // same as int* const k1 = i;
const auto *k2 = i; // same as const int *k2 = i;
In the first case, the object that i points to can be modified through k1, in the second case, it can't.
Hi and welcome to stack overflow.
As this little test program shows, no matter how you specify the type of k, the compiler will never let you lose the constness of i.
#include <iostream>
#include <type_traits>
#include <string>
#define XSTR(s) STR(s)
#define STR(s) #s
template<class T>
struct type;
template<>
struct type<int>
{
static std::string name() { return "int"; }
};
template<class T>
struct type<T&&>
{
static std::string name() { return type<T>::name() + " &&"; }
};
template<class T>
struct type<T&>
{
static std::string name() { return type<T>::name() + " &"; }
};
template<class T>
struct type<T const>
{
static std::string name() { return type<T>::name() + " const"; }
};
#define REPORT_CONST(decl, var, assign) \
{ \
decl var = assign; \
do_it(STR(decl var = assign;), var); \
}
template<class Var>
void do_it(const char* message, Var&&)
{
std::cout << "case: " << message << " results in type: " << type<Var>::name() << '\n';
}
int main()
{
const int i = 42;
REPORT_CONST(const auto &, k, i);
REPORT_CONST(auto &, k, i);
REPORT_CONST(auto &&, k, std::move(i));
REPORT_CONST(auto const &&, k, std::move(i));
REPORT_CONST(int const&, k, i);
// REPORT_CONST(int &, k, i); // error: binding reference of type 'int&' to 'const int' discards qualifiers
}
Expected results:
case: const auto & k = i; results in type: int const &
case: auto & k = i; results in type: int const &
case: auto && k = std::move(i); results in type: int const &
case: auto const && k = std::move(i); results in type: int const &
case: int const& k = i; results in type: int const &
http://coliru.stacked-crooked.com/a/7c72c8ebcf42c351
Note also the decay of named r-values to l-values.
There is a slight difference in first case auto will be deduced to const int and in the second case to int (as you explcitly stated the const).
cpp-reference states
The keyword auto may be accompanied by modifiers, such as const or &, which will participate in the type deduction. For example, given const auto& i = expr;, the type of i is exactly the type of the argument u in an imaginary template template void f(const U& u) if the function call f(expr) was compiled. Therefore, auto&& may be deduced either as an lvalue reference or rvalue reference according to the initializer, which is used in range-based for loop.
So for you this means
// case 1
const int i = 42;
const auto &k = i; // auto -> int
// case 2
const int i = 42;
auto &k = i; // auto -> const int
More important in my opinion is, however, that the intent is stated more clearly if you state const explcitly and the guarantees the constness. Therefore in this case I would clearly prefer it.
The accepted answer is correct, i.e. there is no difference in regards to the compiled result. What's important to note is that the auto& version is coupling the const-ness of the k reference with the const-ness of the variable i. I figured since the question is titled 'Difference between const auto & and auto & ...' then it's important to emphasize the pragmatic difference here, in which if you don't put a the const keyword, you cannot guarantee the reference will have this cv-qualification. In some scenarios where this is desired, why leave it to chance that i will remain const in the future?
In a main function, I created a variable of const int pointer, assign it to a variable declared by auto&. Then using the decltype(x) to check the type. I expected the type is const int*. But is_same returns false.
int main()
{
int a = 10;
const int * cp_val= &a;
auto& x = cp_val;
bool is_const_int_ptr = std::is_same<decltype(x), const int *>::value; // returns 0
// *x = 100; // error: assignment of read-only location '* x'
}
But if I add the following helper function:
#include <boost/type_index.hpp>
template<typename T>
void print_type(T)
{cout << "type T is: "<< boost::typeindex::type_id_with_cvr<T>().pretty_name()<< '\n';}
In the main, I invoke the function
print_type(x); // It returns int const*
Am I missing something in std::is_same?
Note that for auto& x, you're declaring x as reference explicitly; then its type should be const int *&, i.e. a reference to pointer to const int.
Here's a better idea (from Effective Modern C++ (Scott Meyers)) to get the accurate type at compile time from the compiling error message.
template <typename>
struct TD;
then use it as
TD<decltype(x)> td;
you'll get error message like
source_file.cpp:15:21: error: implicit instantiation of undefined template 'TD<const int *&>'
TD<decltype(x)> td;
^
LIVE
Your helper function takes parameter by value; the reference-ness of the argument will be ignored in type deduction, that's why you got const int*.
Template argument deduction and auto are intimately related: The declaration auto x = e; gives x the same type as f(e) would give to T in an invented function template <typename T> f(T);, and similarly for auto& and f(T&), const auto* and f(const T*), etc.
Therefore, to get the correct answer from Boost, you need to declare:
template <typename T> void print_type(T&);
// ^^^^
The type of x is of course const int*&.
Example code:
int aa[] = {1,2,3,4};
auto const p = aa;
Why p deduced as int *const instead int const *?
Because aa is a non-const array, it will decay to pointer-to-non-const and therefore p will be deduced to be a pointer-to-non-const. And because you declared p const, it will be a const pointer-to-non-const.
Declaring a variable const will make that variable const. It doesn't make the type of the variable different (pointer-to-const and pointer-to-non-const are different types) except in the sense that it may be a const qualified version of that same type.
I think there is little more than that, when You wrote:
auto const p = aa;
it means:
auto const p = &aa[0];
so on the right hand side we have pointer to int (not const pointer to int!), so following auto type deduction rules it evaluates to template like this:
template<typename T>
void func_for_ptr(const T* param);
So in our case parameterType is const T*
but T itself is pointer to int.
Constness of type is not changed!
The same effect can be observed other way around, try to define template:
template<typename T>
void foo(const T param) {
int tmp = 0;
T val = &tmp;
*val = 0;
}
int main() {
int *x = new int(1);
foo(x);
}
T was not deduced to pointer to const so You r able to override ptr, but if You will write:
template<typename T>
void foo(T param) {
int tmp = 0;
T val = &tmp;
*val = 0;
}
int main() {
const int *x = new int(1);
foo(x);
}
Type T will be deduced to pointer to const and you will get error. In shortcut auto is always like parameter type.