auto

Given a function with return type as ‘const A&’, if I use ‘auto’ to get the function result as
auto a = func(…);
Will the ‘auto’ be deducted as ‘const A&’ or ‘const A’?

No, it will be just “A” without const or &

#include <iostream>
using namespace std;

const int& func() {return 2;}; //should not do this, because 2 is a local variable…this is just a test of “const”

int main() {
// your code goes here
auto x = func();
x = 1; // x can be assigned with another value
cout << x;
return 0;
}

read more: http://www.cprogramming.com/c++11/c++11-auto-decltype-return-value-after-function.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s