Should I feel “uncomfortable” using auto in C++? [duplicate]

C++11 features the new auto type declaration, allowing you to work with an object without ever knowing its actual type.

I use only strongly typed languages – C++, Delphi (Object Pascal), Go, etc and I feel uncomfortable (guilty?) using auto, (or for example the short variable declaration – x:=1– in Go.) It feels like a hack to me – I use strongly typed languages because they ensure that you know what type you’re using. (With the exception of the abuse of untyped pointers.) Although certainly I appreciate the benefits: Proper type names involving iterators, templates, smart pointers etc can get very lengthy and a bit difficult to determine and declare explicitly, or to parse when reading. Granted, once you know the proper type name, you can “typedef it”, but sometimes getting it right the first time is time consuming and not always so easy.

Or, imagine you that inherit a large, complex project that you need to modify, and every variable possible in that project is declared using auto– you’re going to have to do a lot more work to understand that code-base than if everything was declared using explicit types.

So, what are some guidelines on when to use auto and when to sweat it out with full and proper type names? I am currently reading Stroustrup’s A Tour of C++ and he himself there in Chapter 1 advocates using auto in situations when you know auto will “get it right”:

We use auto where we don’t have a specific reason to mention the type
explicitly. “Specific reasons” include:

• The definition is in a large scope where we want to make the type
clearly visible to readers of our code.

• We want to be explicit about a variable’s range or precision (e.g.,
double rather than float).

In the Advice section of Chapter 1 there he also warns:

-Prefer the {}-initializer syntax for declarations with a named type;

-Prefer the = syntax for the initialization in declarations using
auto;

This, because default initialization {} could result in an incorrect type initialization.

Still, I feel a bit uncomfortable using auto.

Can anyone perhaps give me some additional guidelines about the use of auto, and/or debunk my impression that auto is a hack of sorts and really should be avoided in favor of determining the proper type and then using atypedef?

18

No, you should not feel uncomfortable using auto.

Just use it in situations where the type is obvious, or where no one is going to care about it

A classic example (IMO) of where auto is handy:

std::vector<sometype> vec
...
...
//some code
...
...
for(auto iter = vec.begin(); iter != vec.end(); ++iter)
{
   //something here
}

Nobody really cares about the details of the iterator variable (std::vector<sometype>::iterator iter), only that it is an iterator. Explicitly specifying the type just adds visual noise. Often, without auto, people will create typedefs for commonly used iterators, which can obscure issues and lead to some bizzare error messages if you use the wrong typedef.

4

You are still getting strong types even if they aren’t explicitly specified. You’re going to eventually hit a static type mismatch in most circumstances. The main concern with type inference is accidentally inferring a type too concrete or abstract. In other words, inferring a derived type when you really needed the base type, or vice versa. If it matters what exact type you get, don’t use auto.

Another concern about type inference is if it might change the public interface of your code. In general, you should make the types of your public interface explicit.

Aside from those circumstances, there’s little reason not to use auto when it simplifies your code.

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật