草庐IT

auto_cast_wrapper

全部标签

c++ - 为什么 “cast from ‘X*’ 到 ‘Y’ 失去精度”是一个硬错误,什么是遗留代码的合适修复

1。为什么?像这样的代码曾经有效,它的含义很明显。编译器是否甚至允许(根据规范)让它成为一个错误?我知道它正在失去精度,我很乐意收到警告。但它仍然具有定义明确的语义(至少对于未签名的缩小规模转换已定义)并且用户可能只是想这样做。2。解决方法我有遗留代码,我不想重构太多,因为它相当棘手并且已经调试过了。它正在做两件事:有时将整数存储在指针变量中。如果代码之前在其中存储了一个整数,则该代码只会将指针转换为整数。因此,虽然Actor阵容正在缩小,但现实中永远不会发生溢出。代码已经过测试并且可以工作。当存储整数时,它总是适合普通的旧无符号类型,因此更改类型不是一个好主意并且指针被传递了很多次,

c++ - 未初始化 auto_ptr 时 get() 是否可靠?

考虑以下代码:std::auto_ptrp;if(p.get()==0){...}get()成员函数是否是检查p是否未初始化的标准且可靠的方法?无论平台、编译器、编译器的优化标志等如何,它总是返回0吗? 最佳答案 不存在未未初始化的std::auto_ptr,defaultconstructor将指针初始化为0:explicitauto_ptr(X*p=0);因此get()将在默认构造的std::auto_ptr上有效地返回“0”。 关于c++-未初始化auto_ptr时get()是否可

c++ - 对相同(原始)类型的 static_cast 会产生任何代码吗?

我想这一切都在标题中说了...但这是一个例子。给定voidfunctionThatTakesAFloat(floatpar);floatf=3.5f;做functionThatTakesAFloat(static_cast(f));与相比产生任何额外的代码functionThatTakesAFloat(f);或者这个static_cast是否被编译器完全消除了?编辑:我正在使用VC++(2010) 最佳答案 5.2.9/-2-AnexpressionecanbeexplicitlyconvertedtoatypeTusingasta

c++ - static_cast<char*> 和 (char*) 之间的区别

这是我的第一个问题:)我有一堆文件,我打开它如下图所示;ifstreamin(filename,ios::binary|ios::in)然后,我希望在unsignedinthold中保存2个字节的数据;unsignedinthold;in.read(static_cast(&hold),2);这对我来说似乎是正确的。但是,当我用编译它时g++-ansi-pedantic-errors-Werror--Wall-omainmain.cpp编译器报错error:invalidstatic_castfromtype‘unsignedint*’totype‘char*’其实我已经通过将stat

c++ - 在 std::map 中使用 std::reference_wrapper

我以为maps和reference_wrappers会很容易,但我被一些奇怪的东西绊倒了:#include#includeintmain(void){std::map>mb;constinta=5;mb[0]=std::cref(a);}这段代码给我以下编译器错误:Infileincludedfromc:/MinGW/x86_64-w64-mingw32/include/c++/bits/stl_map.h:63:0,fromc:/MinGW/x86_64-w64-mingw32/include/c++/map:61,from../test/main.cpp:9:c:/MinGW/x8

c++ - 为什么这个 auto_ptr 的 dynamic_cast 会失败?

#include"iostream"classA{private:inta;public:A():a(-1){}intgetA(){returna;}};classA;classB:publicA{private:intb;public:B():b(-1){}intgetB(){returnb;}};intmain(){std::auto_ptra=newA();std::auto_ptrb=dynamic_cast>(a);return0;}错误:不能dynamic_cast`(&a)->std::auto_ptr::get()const 最佳答案

c++ - 结合 boost::lexical_cast 和 std::transform

我想写这样的东西,不能编译:std::vectoras;std::vectorbs(as.size());std::transform(as.beginn(),as.end(),bs.begin(),boost::lexical_cast);但这行不通,所以我创建了一个仿函数来为我做这件事:templatestructlexical_transform{templateDestoperator()(constSrc&src)const{returnboost::lexical_cast(src);}};有更简单的方法吗? 最佳答案

c++ - 如何实现 std::auto_ptr 是复制构造函数?

回到我的疯狂AutoArraythingy...(从那里引用重要的部分:classAutoArray{void*buffer;public://CreatesanewemptyAutoArrayAutoArray();//std::auto_ptrcopysemanticsAutoArray(AutoArray&);//Noteitcan'tbeconstbecausethe"other"reference//isnull'doncopy...AutoArray&operator=(AutoArray);~AutoArray();//Nothrowswap//Note:Atthemom

c++ - 为什么 vector.push_back(auto_ptr) 无法编译?

我了解到STL可以禁止程序员将auto_ptr放入容器中。例如下面的代码不会编译:auto_ptra(newint(10));vector>v;v.push_back(a);auto_ptr有拷贝构造函数,为什么这段代码还能通过? 最佳答案 查看thedefinitionofstd::auto_ptr:namespacestd{templatestructauto_ptr_ref{};templateclassauto_ptr{public:typedefXelement_type;//20.4.5.1construct/copy/

c++ - 诺基亚是否误用了 static_cast?

我刚遇到thisexample:向下滚动到页面底部,您会在此处找到QWidget*pw=static_cast(parent);Parent的类型为:QObject,它是QWidget的基类,所以在这种情况下,不是:应该使用dynamic_cast吗?例如:QWidget*pw=dynamic_cast(parent)谢谢, 最佳答案 如果您知道您是从基类向下转型到子类(即,您知道该对象实际上是子类的一个实例),那么static_cast是完全合法的(并且性能更高)。 关于c++-诺基亚