草庐IT

c++ - MSVC 使用 constexpr 从可变参数模板方法中的基本模板参数中吞下 const if

我有一个问题,我几乎可以肯定这是一个MSVC错误,但也许我遗漏了什么。这是实际代码的简化版本:templateclassInnerType{};templateclassOuterType{public:voidfoo1(){ifconstexpr(true){bar(InnerType());}}voidfoo2(){if(true){bar(InnerType());}}voidbar(InnerType){}};如您所见,foo1()和foo2()之间的唯一区别是constexprif。以下是我尝试在MSVC中编译一些测试时发生的情况:OuterTypetest1;test1.f

指向成员的指针的 C++ 类内初始化使 MSVC 失败(但 GCC/Clang 工作)

这是一个非常简单的C++代码:#includestructA{inta;constexprstaticintA::*p=&A::a;virtualvoidf(){}};intmain(){Ax;x.a=0;x.*(A::p)=1234;std::cout更令人震惊的是,这段代码在GCC、Clang和MSVC之间显示了不同的结果。我试过4个编译器GCC:编译良好,打印1234。Clang:编译良好,打印1234。MSVC(在线):编译失败。带有VisualStudio2019的MSVC(本地):编译良好,打印0。(有趣的是,如果我删除f(),它会打印1234。)我不确定用它自己的成员对指

c++ - 重载 const 和非 const 转换运算符返回数组类型时出现 MSVC 错误 C2593

最近,我尝试使用转换运算符来替代operator[]。喜欢下面的代码:#includeclassfoo{public:usingtype=int[1];public:operatortype&(){returndata;}operatortypeconst&()const{returndata;}private:typedata;};intmain(){foof;f[0]=1;//errorhappensherestd::cout我发现它适用于G++但不适用于MSVCv141(2017)。MSVC报告:errorC2593:'operator['isambiguousnote:coul

c++ - MSVC : modifiers not allowed on non-memberfunction

我在linux下编写了一个信号/插槽库(Codeprojectarticlehere),同时使用Clang3.5和GCC4.9进行编译。它在两个编译器上编译时都没有警告(也在3.4版和4.8版上)。当我完成所有工作并将文章发布到网上时,没过多久我就收到投诉说它不能在MSVC上工作。(VisualStudioExpress2013?对不起,我对版本控制系统不熟悉。)我把它安装在虚拟机中自己看了一下,发现它不会编译以下内容:templatestructRemoveCV;templatestructRemoveCV{usingType=R(Args...);};templatestructR

c++ - 无法使此代码在 MSVC 2015 和 GCC 7.3 之间交叉编译

当我为我的项目编写代码时,我发现了一个非常奇怪的情况,我的C++11代码无法在GCC7.3和之间交叉编译>MSVC2015。案例基本上是这样的://.HfiletemplatestructOuter{Xx={};templatestructInner{Yy={};Inner&operator++();};};//.INLfiletemplatetemplateinlineOuter::Inner&Outer::Inner::operator++(){++y;return*this;}//.CPPfile#includeintmain(){Outer::Innera;++a;std::c

c++ - MSVC中自动生成FMA指令

MSVC多年来一直支持AVX/AVX2指令,并且根据thismsdnblogpost,可以自动生成fused-multiply-add(FMA)说明。然而,以下函数都无法编译为FMA指令:floatfunc1(floatx,floaty,floatz){returnx*y+z;}floatfunc2(floatx,floaty,floatz){returnstd::fma(x,y,z);}更糟糕的是,std::fma不是作为单个FMA指令实现的,它执行得非常糟糕,比普通的x*y+z慢得多(std::fma的糟糕性能是如果实现不依赖于FMA指令,这是预期的)。我用/arch:AVX2/O

c++ - MSVC12 认为从 std::array 派生的聚合不是 pod

鉴于以下#includestructlitmusfinal:std::array{};static_assert(std::is_pod>::value,"notpod");//thisfailsonMSVC:static_assert(std::is_pod::value,"notpod");以下编译器同意litmus是pods:clang++版本3.5(中继线198621)http://coliru.stacked-crooked.com/a/7add7a2fe58a7e38g++4.8.1http://coliru.stacked-crooked.com/a/74cfe97f06

c++ - MSVC++ : Strangeness with unsigned ints and overflow

我有以下代码:#includeusingnamespacestd;intmain(intargc,char*argv[]){stringa="a";for(unsignedinti=a.length()-1;i+1>=1;--i){if(i>=a.length()){cerr如果我在完全优化的MSVC中编译,我得到的输出是“-1?”。如果我在Debug模式下编译(没有优化),我不会得到任何输出(预期。)我认为标准保证无符号整数以可预测的方式溢出,因此当i=(unsignedint)(-1)、i+1=0时,循环条件i+1>=1失败。相反,测试以某种方式通过了。这是编译器错误,还是我在某处

c++ - gcc 和 clang 抛出 "no matching function call"但 msvc (cl) 编译并按预期工作

我写了一个小的函数模板,将不同的容器连接到一个新的容器中:#include#include#include#include#includenamespaceimpl{templatevoidjoin(OutIteratoriterator,constContainer&container,constContainers&...containers){for(constauto&item:container)*iterator++=item;join(iterator,containers...);//gccandclangcannotresolvethiscall}templatevo

c++ - MSVC 中的复合文字

在GCC中,我可以这样做:(CachedPath){ino}inode->data=(structData)DATA_INIT;哪里:structCachedPath{Inoino;};typedefint8_tDepth;structData{Offsetsize;Blknoroot;Depthdepth;};#defineDATA_INIT{0,-1,0}MSVC为这些类型的转换给出了以下错误:errorC2143:syntaxerror:missing';'before'{'我如何在MSVC中执行此操作?请注意,代码已从C99转换而来,我为此使用了指定的初始化程序,然后以类似的方