[EMCpp]Item-7 Distinguish Betwenn () and {} When Creating Objects
· ☕ 4 min read
Braced initialization is the most widely usable initialization syntax, which can also prevent narrowing conversions, as well as being immune to C++’s most vexing parse.
Braced initialization is the most widely usable initialization syntax, which can also prevent narrowing conversions, as well as being immune to C++’s most vexing parse.
“Invisible” proxy types can cause auto to deduce the undesired type for an initializing expression, so we can adopt explicitly typed initializer idiom to force auto to deduce what we want.
Despite some pitfalls described in EMCpp item 2 and 6, auto variables are preferred for they must be initialized, are generally immune to type mismatches that can lead to portability or efficiency problems, can ease the process of refactoring, and typically require less typing than variables with explicitly specified types.
Deduced types can often be seen using IDE editors, compiler error messages, and the Boost TypeIndex library, but the results of some tools may be neither helpful nor accurate.
decltype almost always yields the type of a variable or expression without any modifications. For lvalue expressions of type T other than names, decltype always reports a type of T&.