草庐IT

Duck-typing

全部标签

C++ 模板 : how to determine if a type is suitable for subclassing

假设我有一些模板类取决于类型T.T几乎可以是任何东西:int,int*,pair或structlol;它不能是void,引用或任何cv合格的东西。对于一些优化,我需要知道我是否可以子类化T.所以,我需要一些特征类型is_subclassable,确定为基本特征的逻辑组合或通过一些SFINAE技巧。在原来的例子中,int和int*不可子类化,而pair和structlol是。编辑:正如litb在下面指出的,union也不可子类化并且T也可以是union类型。如何编写我需要的特征类型? 最佳答案 你要判断是否是非union类。我不知道有

c++ - 以指针为键的 map 的 value_type

据我所知,C++定义了map::value_type作为pair如果我在map中使用指针类型作为键类型会发生什么,即是std::map::value_type::first_type=constchar*正如我从上面的定义中所期望的那样std::map::value_type::first_type=constchar*const这样会更合乎逻辑(因为否则我将被允许从映射迭代器更改键值)? 最佳答案 您的推理是正确的,value_type::first将是charconst*const。当T是type*时,认为constT是cons

c++ - "error: cannot use type ' void' as a range"究竟是什么意思?

当我在clang3.2中编译它时for(autox:{1,1.2}){}我收到这样的错误:error:cannotusetype'void'asarange这是什么意思? 最佳答案 您在初始化列表中混合了您的类型。在这种情况下它可以很清楚,但不要忘记std::stringfoo;for(autox:{foo,"bar"}){}也是两种不同的类型。当然还有很多其他情况,您可能希望它起作用,但类型必须完全匹配。 关于c++-"error:cannotusetype'void'asarange

java - 没有 WindowManager.LayoutParams.TYPE_PHONE 的粘性覆盖

粘性是指调用启动器Intent(intent.addCategory(Intent.CATEGORY_HOME)时不会关闭的窗口。以前这是使用WindowManager.LayoutParams.TYPE_PHONE完成的,但现在已弃用此类型并在api28上引发异常:WindowManager$BadTokenException...permissiondeniedforwindowtype2002这种行为仍然是可能的,因为Facebook的Messenger使用它的聊天“Heads”来实现它,这是基于Facebook没有获得系统应用程序权限的假设,因为它预装在很多rom上。使用Win

java - 没有 WindowManager.LayoutParams.TYPE_PHONE 的粘性覆盖

粘性是指调用启动器Intent(intent.addCategory(Intent.CATEGORY_HOME)时不会关闭的窗口。以前这是使用WindowManager.LayoutParams.TYPE_PHONE完成的,但现在已弃用此类型并在api28上引发异常:WindowManager$BadTokenException...permissiondeniedforwindowtype2002这种行为仍然是可能的,因为Facebook的Messenger使用它的聊天“Heads”来实现它,这是基于Facebook没有获得系统应用程序权限的假设,因为它预装在很多rom上。使用Win

c++ - 继承 : Function that returns self type?

假设我有两个类:classA{public:A*Hello(){returnthis;}}classB:publicclassA{public:B*World(){returnthis;}}假设我有一个B类的实例,如下所示:Btest;如果我调用test.World()->Hello()就可以了。但是test.Hello()->World()将无法工作,因为Hello()返回A类型。如何让Hello()返回B的类型?我不想使用virtual函数,因为我们有20多个不同的类继承A。 最佳答案 您可以使用CRTP,奇怪的重复模板模式:t

c++ - std::is_constructible on type with non-public destructor

std::is_constructible的预期结果是什么?在具有私有(private)或protected析构函数的类型上?例如,即使只有friend可以释放它,我仍然可以在堆上构造这样一个对象:#includeclassFoo{friendvoidfreeFoo(Foo*);public:Foo(){}private://Destructorisprivate!~Foo(){}};voidfreeFoo(Foo*f){deletef;//deletingafooisfineherebecauseoffriendship}intmain(){Foo*f=newFoo();//dele

c++ - 错误 : ‘ostream’ does not name a type

我正在重载C++中的>运算符,但它无法编译。错误信息是:“error:‘ostream’doesnotnameatype”为什么会出现此错误?如何解决?#ifndefCOMPLEX_H#defineCOMPLEX_H#include//exit#include#includeclassComplex{public:Complex(void);Complex(doublea,doubleb);Complex(doublea);doublereal()const{returna;}doubleimag()const{returnb;}friendostream&operator>(istr

c++ - 警告 : pointer of type ‘void *’ used in arithmetic

我正在从内存映射中写入和读取寄存器,如下所示://READreturn*((volatileuint32_t*)(map+offset));//WRITE*((volatileuint32_t*)(map+offset))=value;但是编译器给我这样的警告:warning:pointeroftype‘void*’usedinarithmetic[-Wpointer-arith]如何更改我的代码以删除警告?我正在使用C++和Linux。 最佳答案 因为void*是一个指向未知类型的指针,所以你不能对它进行指针运算,因为编译器不知道

c++ - 帮助解决错误 : ISO C++ forbids declaration of 'vector' with no type

如标题所述,我不确定为什么会收到此错误。我整理了一个类似于此结构的test.cpp,它工作正常。此外,除了vector问题之外,还有另一个关于“protected”的问题,它甚至不在代码中。我认为“protected”是一个宏,所以不知道那里有什么。我是QT的新手,所以我很可能“做错了”。这当然是编译器的建议。InfileincludedfromDrvCrystalfontz.cpp:8:LCDText.h:28:error:ISOC++forbidsdeclarationof'vector'withnotypeLCDText.h:28:error:expected';'before'