When I use C++11 auto
, what are the rules of type deduction with regards to whether it will resolve to a value or a reference?
E.g, sometimes it is clear:
auto i = v.begin(); // Copy, begin() returns an iterator by value
These are less clear:
const std::shared_ptr<Foo>& get_foo();
auto p = get_foo(); // Copy or reference?
static std::shared_ptr<Foo> s_foo;
auto sp = s_foo; // Copy or reference?
std::vector<std::shared_ptr<Foo>> c;
for (auto foo: c) { // Copy for every loop iteration?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…