草庐IT

c++ - 无符号和有符号比较

这里是非常简单的代码,#includeusingnamespacestd;intmain(){unsignedintu=10;inti;intcount=0;for(i=-1;icount的值为0,为什么? 最佳答案 的两个操作数必须提升为同一类型。显然他们被提升为unsignedint(我面前没有标准中的规则,稍后我会查找)。自(unsignedint)(-1)为假,循环永远不会执行。该规则可在标准第5节(expr)的第10段中找到,其中指出(我已突出显示此处适用的规则):Manybinaryoperatorsthatexpect

c++ - 如何在 C++03 中使用 "dereference a type"?

如何在C++03中获取另一种类型的“解引用类型”?请注意,它可以是其他可取消引用的类型,例如std::vector::iterator.例如如果我有templatestructMyPointer{Tp;???operator*(){return*p;}};我怎样才能弄清楚用什么替换???和?(没有提升!我想知道如何自己解决。) 最佳答案 templatestructdereference;templatestructdereference{typedeftypenameTtype;};templatestructMyPointer{

c++ - 允许访问私有(private)成员

这个问题在某种程度上是thisone的延续我已经发布了。我想做的:我的意思是允许访问基类的私有(private)成员A在派生类中B,具有以下限制:我想访问的是一个结构--std::map,实际上——,不是一种方法;我不能修改基类;基类A没有模板化的方法,我可以作为后门替代方案重载——我不会添加这样的方法,因为它会反对第二个约束。作为一个可能的解决方案,有人指出我是litb的解决方案(post/blog),但是,对于我来说,我一直无法理解这些帖子中所做的事情,因此,我无法得出解决问题的方法。我正在尝试做的事情:以下代码来自litb的解决方案,提供了一种如何从类/结构访问私有(privat

c++ - 错误 : ‘i’ does not name a type with auto

这个问题在这里已经有了答案:HowdoIenableC++11ingcc?(4个答案)关闭7年前。我是C++新手,这是我的程序#include#include#include#include#includeintmain(){staticconstdoublearr[]={16.0,2.2,77.5,29.0,24.0};std::vectorvec(arr,arr+sizeof(arr)/sizeof(arr[0]));std::transform(vec.begin(),vec.end(),vec.begin(),bind2nd(std::minus(),3.0));for(aut

c++ - 从 std::array 等获取 size_type 的惯用方法

由thisquestion触发,我想出了以下代码(在我的回答中是boost::array,但同样适用于std::array):template::size_typesize>voidDataTransform(std::arraydata){}我对::size_type一点都不满意.我必须以一定的大小进行实例化,才能知道size_type.对于std::array我本可以使用size_t,那么一般情况呢?如果size_type怎么办不是size_t?或者更一般的(即不适用于std::array)如果size_type怎么办?不同的尺寸是不同的(愚蠢但可能)?我知道这个问题相当学术,有很

c++ - 错误 : '...' does not name a type

我有一个工作项目。重新安排一些代码后,我尝试重新编译我的项目,然后奇怪的事情开始发生。查看编译器输出的这段摘录。我正在使用MinGWG++从Windows上的Eclipse进行编译。****BuildofconfigurationDebugforprojectPract2********InternalBuilderisusedforbuild****g++-O0-g3-Wall-c-fmessage-length=0-omove.o..\move.cppInfileincludedfrom..\/game.h:11:0,from..\/piece.h:10,from..\/move.

c++ - C89 或 C++03 是否定义了严格的别名规则?

我看过severalassertionsC89和C++03定义了严格的别名规则。但是,我无法在标准中找到该特定位。我的理解是C99中添加了严格的别名规则。 最佳答案 此文本出现在C89,§3.3表达式中:Anobjectshallhaveitsstoredvalueaccessedonlybyanlvaluethathasoneofthefollowingtypes:thedeclaredtypeoftheobject,aqualifiedversionofthedeclaredtypeoftheobject,atypethatis

为什么我遇到错误:attributeError:'int'对象没有属性'type'

我正在将LLVMlite与Pyvex结合使用。我已经在llvmlite中定义了一些功能,如下所示:defint32(val):returnir.Constant(ir.IntType(32),val)defput64(putoffset,val):llvmtmp=builder.gep(regtag,(int32(0),int32(putoffset)),True)returnbuilder.store(val,llvmtmp)但是,当我想使用以下代码调用此函数时:forstmtinirsb.statements:ifisinstance(stmt,pyvex.IRStmt.Put):puto

c++ - 在模板中返回 std::set<T>::iterator 时出错

我正在围绕std::set制作一个模板包装器。为什么Begin()函数声明会出错?templateclassCSafeSet{public:CSafeSet();~CSafeSet();std::set::iteratorBegin();private:std::set_Set;};错误:类型“std::set,std::allocator>”不是从类型“CSafeSet”派生的 最佳答案 尝试typename:templateclassCSafeSet{public:CSafeSet();~CSafeSet();typenames

C++11 decltype : How to declare the type that a pointer points to?

我有以下代码:#includeintmain(){int*a=newint(2);std::unique_ptrp(a);}导致这些错误信息:Infileincludedfroma.cpp:1:Infileincludedfrom/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/memory:81:/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.2/../../../../include/c++/4.9.2/bits/