草庐IT

const_cast-ing

全部标签

c++ - 基于范围的 for 循环与 const shared_ptr<>

我有一个容器shared_ptr,例如一个vector>v我想遍历v表示常量。这段代码:vector>v;v.push_back(make_shared("hallo"));...for(constauto&s:v){*s+=".";//看起来就像我想做的(表示s是const)但当然它不会生成字符串const.有没有一种优雅的方法来遍历shared_ptr的容器?这表明内容不会被修改?有点像for(shared_ptrs:v){*s+=".";//(但由于其他原因这段代码无法编译:))编辑:我错了。最初我是在声明一个引用,这导致编译器错误for(shared_ptr&s:v){//如果

c++ - 为什么在这种情况下调用非 const 右值 move 构造函数?

我看过相关的问题,他们主要讨论我们是否应该将const右值引用作为参数。但我仍然无法理解为什么在以下代码中调用了非常量move构造函数:#includeusingnamespacestd;classA{public:A(intconst&&i){cout 最佳答案 这里是你的代码的一个稍微修改的版本:#include#if0usingT=int;#elsestructT{T(int){}};#endifusingnamespacestd;classA{public:A(Tconst&&i){cout当T==int时,您会得到非常量重

C++ 范围-v3 库 : 'take' -ing first 3 perfect numbers works and halts; 'take' -ing first 4 doesn't stop after 4

据我了解,range-v3库的View操作(目前需要C++17,但要成为C++20中STL的正式部分)提供了可链接的类STL算法,这些算法是延迟计算的。作为实验,我创建了以下代码来评估前4个完全数:#include#includeusingnamespacestd;intmain(intargc,char*argv[]){autoperfects=ranges::view::ints(1)|ranges::view::filter([](intx){intpsum=0;for(inty=1;y代码以可能无限范围的数字开始(ranges::view::ints(1)),但是因为View算

c++ - 我是否将 'const' 放入我的 UML 图中?

我正在使用Dia制作UML图.当函数是const时,我是否需要在图中放置const?如果有,在哪里? 最佳答案 latestUMLspecification中的第11.8.2章(“操作”)将isQuery列为操作的属性之一:isQuery:Boolean-SpecifieswhetheranexecutionoftheOperationleavesthestateofthesystemunchanged(isQuery=true)orwhethersideeffectsmayoccur(isQuery=false).Thedefau

c++ - 如何将 'const boost::filesystem2::path' 变成 'const char *' ?

如何将“constboost::filesystem2::path”转换为“constchar*”? 最佳答案 尝试使用path::string().c_str() 关于c++-如何将'constboost::filesystem2::path'变成'constchar*'?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4875482/

c++ - 头文件中的 `static` , `extern` , `const`

//a.hexternintx1;staticintx2;intx3;staticconstintx4;classA{public:staticconstintx5=10;};a.h会被多个.cpp文件包含,我的问题是:1.x1只是一个声明,不是吗?所以它的定义应该在那些.cpp文件之一中完成,对吧?2.x2是一个定义,对吧?我曾经认为staticint和externint一样也是一个声明,但我错了。x2将仅在a.h中可见?3.如果a.h包含在多个.cpp文件中,x3会被定义多次,所以x3会导致编译错误,对吧?4.x4是一个定义,对吧?5.这里在A类中,x5是一个声明,是的。但是x4呢

c++ - 带有 C++ 代码的 Const 和 weak 属性

我无法理解下面的编译错误。第一个文件是一个头文件,test_weak.h:#ifndefTEST_WEAK_H#defineTEST_WEAK_H#ifndef__ASSEMBLER__constchar*constTUTU__attribute__((weak))="TUTU";constchar*TUTU_DATE__attribute__((weak))=__DATE__;constchar*consttutu="tutu";#endif/*ASSEMBLER*/#endif/*TEST_WEAK_H*/第二个文件是主要的test.cpp:intmain(){return42;}

c++ - reinterpret_cast 与严格别名

我正在阅读有关严格别名的内容,但它仍然有点模糊,我不确定定义/未定义行为的界限在哪里。最详细post我发现专注于C。所以如果你能告诉我这是否被允许以及自C++98/11/...以来发生了什么变化,那就太好了#include#includetemplateTtransform(Tt);structmy_buffer{chardata[128];unsignedpos;my_buffer():pos(0){}voidrewind(){pos=0;}templatevoidpush_via_pointer_cast(constT&t){*reinterpret_cast(&data[pos]

c++ - std::map<int, int> 和 std::map<const int, int> 有区别吗?

据我了解,std::map中的值对中的键一旦插入就无法更改。这是否意味着使用const键模板参数创建映射没有效果?std::mapmap1;std::mapmap2; 最佳答案 您的标题问题的答案是肯定的。它们是有区别的。你不能传递std::map到接受std::map的函数.然而,map的功能行为是相同的,即使它们是不同的类型。这并不罕见。在许多情况下,int和long的行为相同,即使它们在形式上是不同的类型。 关于c++-std::map和std::map有区别吗?,我们在Stack

c++ - 你会把 C++ RValue 引用参数标记为 const

我一直在切换模板工厂函数以使用(并理解)std::forward以支持右值和移动语义。我通常用于模板类的样板工厂函数总是将参数标记为const:#include#includetemplatestructMyPair{MyPair(constT&t,constU&u):t(t),u(u){};Tt;Uu;};templatestd::ostream&operator&pair){os"MyPairMakeMyPair(constT&t,constU&u){returnMyPair(t,u);}usingnamespacestd;intmain(intargc,char*argv[]){