25
是否有區別或是否相同?'auto const'和'const auto'是否一樣?
是否有區別或是否相同?'auto const'和'const auto'是否一樣?
const
限定符適用於直接左側的類型,除非左側沒有任何內容,則它適用於右側的類型。所以,它是一樣的。
人爲的例子:
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
兩個a
和b
具有類型char* const
。不要以爲你可以簡單地「插入」類型而不是關鍵字auto(這裏:const char* a
)! const
關鍵字將應用於auto
匹配的整個類型(這裏是:char*
)。
和其他類型一樣。 – chris