草庐IT

qvariant_cast

全部标签

c++ - 为什么 std::bad_cast 被 boost::locale 抛出?

我试图了解如何使用boost::locale来比较忽略大小写和变体的字符串。我直接尝试了Boost文档中的代码:http://www.boost.org/doc/libs/1_51_0/libs/locale/doc/html/collation.htmlboost::locale::generatorgen;std::localevLocale=gen("");std::wstringa=L"Façade",b=L"facade";//Followingwillthrowbad_castbooleq=std::use_facet>(vLocale).compare(boost::lo

c++ - Qt:将数字转换为 QString、QVariant 或 QString::number 哪个更好

我只是好奇。比方说,我需要在控制台中输出一个数字。代码为:#include#include#includevoiddisplayNumber(quint8number){qDebug()哪个性能更好?我认为内存消耗也不同。QVariant(number).toString()意味着它将QVariant存储在堆栈中。不确定QString::number(),它不应该只调用该函数(当然,该函数有一个QString返回,所以它也在堆栈上分配并占用该空间和分配和取消分配它的操作)?无论如何,sizeof()为QVariant提供了16个字节,为QString提供了4个字节。

c++ - 带有 std::shared_ptr 的 QVariant

我有以下问题,我正在使用Q_DECLARE_METATYPE(std::shared_ptr);qRegisterMetaType>();QMetaType::registerComparators>();使用std::shared_ptr与例如QList模型。我需要一种行为QVariant::fromValue(std::shared_ptr(newint(5)))==QVariant::fromValue(std::shared_ptr(newint(5)))是真的。自std::shared_ptr::operator==()以来,我上面的代码在这里返回false比较原始指针。是否

c++ - std::chrono::duration_cast - GCC 实现有问题吗?

我想我在std::chrono::duration_cast的GCC实现中发现了一个错误。谁能证实我这一点?测试代码:usingTicks=std::chrono::duration>>;usingdur=typenamestd::chrono::system_clock::duration;intmain(){std::chrono::time_pointearliest{std::chrono::duration_cast(std::chrono::time_point::max().time_since_epoch())};autoticks=std::chrono::time_

c++ - dynamic_cast 真的适用于多重继承吗?

我想看看是否可以创建“接口(interface)”,继承它们,然后在运行时检查是否有任何随机类实现了该接口(interface)。这是我的:structGameObject{intx,y;std::stringname;virtualvoidblah(){};};structAirholder{intoxygen;intnitrogen;};structTurf:publicGameObject,publicAirholder{Turf():GameObject(){name="Turf";}voidblah(){};};voidremove_air(GameObject*o){Air

c++ - 铿锵错误 : ambiguous conversion for static_cast

我有以下代码:typedefintAliasB;typedefunsignedshortAliasA;classAlias{public:explicitAlias(intsomeInt){}};//(*)!!belowbreakstheconversionpathviaAliasA!!//typedefAliasAliasA;classC{public:C(){}};classB{public:B(){}B(constAliasB&value){}operatorAliasB()const{return-1000;}Ccombine(constB&someB){returnC();}

C++11 Dynamic Cast If Else Chain -> 开关

考虑以下几点:structB{};templatestructD:B{Tt;}voidg(inti){...}voidg(strings){...}voidg(charc){...}voidf(B*b){if(dynamic_cast*>(b)){g(dynamic_cast*>(b)->t);}elseif(dynamic_cast*>(b)){g(dynamic_cast*>(b)->t);}elseif(dynamic_cast*>(b)){g(dynamic_cast*>(c)->t)}elsethrowerror;};这里只有三种可能的T类型——int、string、char

c++ - 为什么允许将 void* 的 static_cast 转换为另一种类型?

我刚刚在读这个帖子:Simplec++pointercasting这让我开始思考为什么不允许在不同指针类型之间进行static_cast(允许的情况除外)除非您将static_cast转换为void*作为中间步骤。在我看来,要么两者都被允许,要么两者都不被允许。这是一个例子:char*cs;unsignedchar*ucs;cs=reinterpret_cast(ucs);//1)allowed,ofcoursecs=static_cast(ucs);//2)notallowed:incompatiblepointertypescs=static_cast(static_cast(uc

c++ - boost::lexical_cast<> 的语言环境不变保证

我正在使用boost::lexical_cast(double)用于将double转换为字符串,生成JSON序列化字节流,即(在远程端)由.NET解析。我能够强制.NET使用InvariantCulture用于解析,从而在每种可能的语言上返回可预测的结果。但是,我无法在boost::lexical_cast中找到此保证文档。我试了一下,对于不同的区域设置,它的工作方式相同。但是,我不能仅从几个测试中确定,我是否遗漏了文档中的某些内容,或者根本无法保证这一点,我必须使用其他东西吗?编辑:我发现了一个问题。std::locale::global(std::locale("Czech"));

c++ - 这个 reinterpret_cast 可以吗

我是EE,不是代码专家,所以请多多包涵。我正在使用EmbarcaderoC++Builder(XE3)。我有一个FFT算法,它对复数进行大量运算。我发现如果我绕过Embarcadero的复杂数学库,并在我自己的代码中进行所有计算,我的FFT将运行大约4.5倍。此处显示的4个操作都需要大量时间。#include#defineComplexDstd::complexComplexDX,Y,Z,FFTInput[1024];doublex,y;Z=X*Y;x=X.real();y=X.imag();Z=ComplexD(x,y);用我自己的交叉乘法代替乘法将我的执行时间减半。然而,我关心的是