Search code examples
c++resharperconstantsauto

Resharper telling me to use auto with const


In my program, resharper is telling me that I should use auto whenever I initialize a const value, such as the int charSize I declared. Why should I use auto instead of an int when I know that I want that to be a integer, not any other datatype because I won't ever change that value from the int type. Is it more efficient to use the auto type instead of a direct type?

#include <iostream>

int main()
{
    const int charSize = 256;

    char str[charSize];

    scanf_s("%256s", str, charSize);

    printf("%s\n", str);
}

Solution

  • There is no "auto type". Writing auto since C++11 enables type deduction, so the object would still be an int but your compiler would have figured that out for you.

    In this example, there is no benefit to that whatsoever; in fact, simply being clear and writing the type you want is (IMO) far superior. If it's not simply buggy, Resharper is overstepping its bounds in trying to make that decision for you.

    It's possible that Resharper is confused, and trying to get you to use the auto storage class specifier (removed in C++11), though even for const objects this was always the default at block scope.