c++ - Usage of auto for global constants -
suppose have this:
namespace { const unsigned my_uint = 100u; const float my_float = 0.f; const char* my_string = "hello world"; }
do expected behavior using auto
these? presume improvement, i'm not sure in practice.
namespace { auto my_uint = 100u; auto my_float = 0.f; auto my_string = "hello world"; }
are 2 code examples semantically same? these const
automatically? if not, should specify auto const
?
auto
's deduction rules equivalent by-value template argument deduction. creating object value entails stripping references , top-level cv-qualifiers initializer. 2 examples not equivalent. in particular, primitive 100u
of type unsigned int
, that's deduced as. likewise, 0.f
of type float
.
adding const
makes sense if variable itself not modified. if want make constants in program, using constexpr
might better.
Comments
Post a Comment