C++ 我可以在指针的副本上使用const_cast吗?

C++ 我可以在指针的副本上使用const_cast吗?,c++,constants,C++,Constants,这里有未定义的行为吗 #include <iostream> #include <string> int main() { std::string str = "Hello"; auto p = str.c_str(); char x[] = "sup"; const_cast<char*&>(p) = x; } #包括 #包括 int main() { std::string str=“Hello”; 自动p=

这里有未定义的行为吗

#include <iostream>
#include <string>

int main()
{
    std::string str = "Hello";

    auto p = str.c_str();
    char x[] = "sup";

    const_cast<char*&>(p) = x;
}
#包括
#包括
int main()
{
std::string str=“Hello”;
自动p=str.c_str();
字符x[]=“sup”;
const_cast(p)=x;
}

不,只有舍弃固有常量对象的常量,并对其进行修改,您才能获得UB,而不是其他

不,它仍然指向不可修改的内存。如果您正在进行
std::string
编辑,则可以在C++11中安全地使用
&str[0]
。这有关系吗?
const_cast
在那里是毫无意义的,因为
p=x非常好…@chris:它没有指向“const”memory@DavidRodríguez dribeas,不在这里,不,但是对于标题中的问题,它是否是副本并不重要。我不太清楚为什么这个问题与代码不匹配,但这是我在阅读时首先想到的。