我认为问题很清楚.将auto关键字自动检测常量性,或总是返回一个非const类型,即使有如.函数的两个版本(一个返回const,另一个不返回).
仅仅为了记录,我确实const auto end = some_container.end()在我的for循环之前使用,但我不知道这是否必要或甚至与正常情况不同auto.
我尝试了一些代码,并想知道constC++中的限定符在使用时如何应用于指针类型auto.
int main()
{
int foo = 1;
int bar = 2;
//Expected: const int * ptr_to_const_int = &foo;
const auto ptr_to_const_int = &foo;
//Expected: int * const const_ptr_to_int = &foo;
auto const const_ptr_to_int = &foo;
*ptr_to_const_int = 3; //Thought this would error
//ptr_to_const_int = &bar; This does error.
*const_ptr_to_int = 3;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我意识到有一个类似的问题,询问它们是否相同,我更具体地询问这里的规则是什么,它适用于推断结束指针类型.