Is 'auto const' and 'const auto' the same?

前端 未结 2 1493
北荒
北荒 2020-12-25 09:27

Is there a semantic difference between auto const and const auto, or do they mean the same thing?

相关标签:
2条回答
  • 2020-12-25 10:03

    The const qualifier applies to the type to the immediate left unless there is nothing to the left then it applies to the type to the immediate right. So yup it's the same.

    0 讨论(0)
  • 2020-12-25 10:07

    Contrived example:

    std::vector<char*> test;
    const auto a = test[0];
    *a = 'c';
    a = 0; // does not compile
    auto const b = test[1];
    *b = 'c';
    b = 0; // does not compile
    

    Both a and b have type char* const. Don't think you can simply "insert" the type instead of the keyword auto (here: const char* a)! The const keyword will apply to the whole type that auto matches (here: char*).

    0 讨论(0)
提交回复
热议问题