草庐IT

const_cast-ing

全部标签

c++ - 在 C++ 中更改 const 变量的值

这个问题在这里已经有了答案:WeirdBehaviourwithconst_cast[duplicate](2个回答)关闭6年前。我正在尝试更改定义为intconst的变量的值,如下所示。constintw=10;int*wp=const_cast(&w);*wp=20;w的值没有改变,即使在赋值之后也是10,尽管它表明w和wp都指向同一个内存位置。但是如果在声明时定义如下,我可以更改w的值inti=10;constintw=i;如果我更改i的声明以使其像in一样constconstinti=10;w的值不变。在第一种情况下,为什么w的值没有改变,即使w和wp指向同一个内存位置[这是我

c++ - 在 C++ 中更改 const 变量的值

这个问题在这里已经有了答案:WeirdBehaviourwithconst_cast[duplicate](2个回答)关闭6年前。我正在尝试更改定义为intconst的变量的值,如下所示。constintw=10;int*wp=const_cast(&w);*wp=20;w的值没有改变,即使在赋值之后也是10,尽管它表明w和wp都指向同一个内存位置。但是如果在声明时定义如下,我可以更改w的值inti=10;constintw=i;如果我更改i的声明以使其像in一样constconstinti=10;w的值不变。在第一种情况下,为什么w的值没有改变,即使w和wp指向同一个内存位置[这是我

c++ - std::function 中的 const 和 & 被忽略了吗?

为什么这段代码会编译?std::functionf=[](inta){};int和constint&不是不同的类型吗? 最佳答案 它们是不同的,但没关系,因为constint&参数可以传递给int参数,这就是所需要的。 关于c++-std::function中的const和&被忽略了吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/45387506/

c++ - std::function 中的 const 和 & 被忽略了吗?

为什么这段代码会编译?std::functionf=[](inta){};int和constint&不是不同的类型吗? 最佳答案 它们是不同的,但没关系,因为constint&参数可以传递给int参数,这就是所需要的。 关于c++-std::function中的const和&被忽略了吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/45387506/

c++ - 如何编写可以采用 const 迭代器的 C++11 模板

回复thisquestion在CodeReview上,我正在考虑如何编写模板函数来指示所包含对象的const-ness。具体来说,考虑这个模板化函数#include#include#includetemplatetypenamestd::iterator_traits::value_typeaverage(Itbegin,Itend){typedeftypenamestd::iterator_traits::value_typereal;realsum=real();unsignedcount=0;for(;begin!=end;++begin,++count)sum+=*begin;

c++ - 如何编写可以采用 const 迭代器的 C++11 模板

回复thisquestion在CodeReview上,我正在考虑如何编写模板函数来指示所包含对象的const-ness。具体来说,考虑这个模板化函数#include#include#includetemplatetypenamestd::iterator_traits::value_typeaverage(Itbegin,Itend){typedeftypenamestd::iterator_traits::value_typereal;realsum=real();unsignedcount=0;for(;begin!=end;++begin,++count)sum+=*begin;

c++ - 为什么强类型枚举可以用没有static_cast的整数初始化?

enumclassE{};intmain(){Ee1{0};//okEe2=0;//notok//error:cannotinitializeavariableof//type'E'withanrvalueoftype'int'}我的编译器是clang4.0,带有选项-std=c++1z。预计Ee2=0;不行,因为E是强类型的。不过,让我吃惊的是Ee1{0};应该没问题。为什么没有static_cast可以用整数初始化强类型枚举? 最佳答案 看reference自C++17起允许使用列表初始化器:Bothscopedenumerat

c++ - 为什么强类型枚举可以用没有static_cast的整数初始化?

enumclassE{};intmain(){Ee1{0};//okEe2=0;//notok//error:cannotinitializeavariableof//type'E'withanrvalueoftype'int'}我的编译器是clang4.0,带有选项-std=c++1z。预计Ee2=0;不行,因为E是强类型的。不过,让我吃惊的是Ee1{0};应该没问题。为什么没有static_cast可以用整数初始化强类型枚举? 最佳答案 看reference自C++17起允许使用列表初始化器:Bothscopedenumerat

c++ - 带有 const 引用的 std::remove_const

为什么std::remove_const不能将constT&转换为T&?这个公认的相当人为的例子证明了我的问题:#includeintmain(){inta=42;std::remove_const::typeb(a);//Thisassertionfailsstatic_assert(!std::is_same::value,"Whydidremove_constnotremoveconst?");return0;}上面的情况很容易解决,所以对于上下文,想象一下:#includetemplatestructSelector{constexprstaticconstchar*value

c++ - 带有 const 引用的 std::remove_const

为什么std::remove_const不能将constT&转换为T&?这个公认的相当人为的例子证明了我的问题:#includeintmain(){inta=42;std::remove_const::typeb(a);//Thisassertionfailsstatic_assert(!std::is_same::value,"Whydidremove_constnotremoveconst?");return0;}上面的情况很容易解决,所以对于上下文,想象一下:#includetemplatestructSelector{constexprstaticconstchar*value