Are there any downsides to marking all variables you don’t modify const?

There are no downsides to marking variables you don’t modify const.

There are some up-sides though: the compiler will help you diagnose when you unintentionally modify a variable you shouldn’t/didn’t mean to and the compiler may (although due to the language having const_cast and mutable this is rare) generate better code.

So, I’d advise; use const where you can. There are no downsides and your compiler can potentially help you spot bugs. No reason not to (except for a bit of extra typing).

Note that this extends to member functions as well. Make them const when you can – it lets them be used in more contexts and helps users reason about the code (“calling this function won’t modify the object” is valuable information).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)