草庐IT

c++ - "Why switch statement cannot be applied on strings?"的答案是否仍然正确,即使使用 C++11/14?

我遇到了这个问题:Whyswitchstatementcannotbeappliedonstrings?并想知道答案是否:Thereasonwhyhastodowiththetypesystem.C/C++doesn'treallysupportstringsasatype.Itdoessupporttheideaofaconstantchararraybutitdoesn'treallyfullyunderstandthenotionofastring.仍然适用,即使在C++11/14中使用std:string。是否有多个elseif(...)的替代方案?

c++11: 委托(delegate)构造函数 - 无法选择构造函数模板?

这是后续问题:c++11dedicated"proxyconstructors"delegatingtoprivateuniveralreferenceconstructor?我想去掉那里使用的“enumclassDummy”。但我没有设法委托(delegate)给模板构造函数。请参见下面的代码示例。#include#include#includeclassMyClass{private:templateMyClass(T&&data):_data(std::forward(data)){std::cout(data){std::cout(std::move(data)){std::c

C++11:函数模板:通过引用传递参数

有一种模板函数通过引用传递参数+回调函数,但是如果参数通过引用传递给回调函数则编译器会抛出错误:nomatchingfunctionforcalltofunc(int&,void(&)(int&)).怎么了?templateusingfunc_t=void(T);templatevoidfunc(T&arg,func_tcallback){callback(arg);}voidfunc1(intarg){}voidfunc2(int&arg){}// 最佳答案 扣除T在第二次调用中失败,因为T出现在两个推断的上下文中,这两个上下文推

c++ - 警告 C++11 继承的构造函数

https://en.wikipedia.org/wiki/C++11#Object_construction_improvementForbase-classconstructors,C++11allowsaclasstospecifythatbaseclassconstructorswillbeinherited.Thus,theC++11compilerwillgeneratecodetoperformtheinheritanceandtheforwardingofthederivedclasstothebaseclass.Thisisanall-or-nothingfeatur

c++ - 简单的 DirectX 11 程序运行时错误

我是DirectX11的新手,我已经设法在我的书中找到了绘制三角形一章。我写了这段代码,但出现运行时错误:“DirectX11.exe中0x00cd14e6处的未处理异常:0xc0000005:访问冲突读取位置0x00000000。”我的编译器显示错误出在InitPipeline函数中,但我找不到那里的错误。我做错了什么?代码:#include#include#include#include#include#pragmacomment(lib,"d3d11.lib")#pragmacomment(lib,"d3dx11.lib")#pragmacomment(lib,"d3dx10.l

c++ - Visual Studio 11 Developer Preview 的开发适用性

我最近下载了VisualStudio11DeveloperPreview,它看起来棒极了。我阅读了下载页面上的细则,上面写着:VisualStudio11DeveloperPreviewisprereleasesoftwareandshouldnotbeusedinproductionscenarios.我将仅使用VS11的C++编译器/IDE,所以有人知道有哪些错误会阻止我在生产场景中使用VS11吗?我真的很想开始使用它,我不知道是C++部分没有完成,还是只是一些与我无关的不适合该领域的TeamFoundationSomethingorother。 最佳答

c++ - 不持有锁的本地静态初始化避免了 C++11 中可能出现的死锁?

论文http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2660.htm提出了一种算法,该算法在局部静态变量的初始化期间不需要持有锁,但仍会导致通过变量定义的并发控制流等待初始化完成。论文说这样做的好处是避免了可能出现的死锁Thecoreproblemwithfunction-localstatic-durationobjectinitializationisthatthecontainingfunctionmaybeinvokedconcurrently,andthusthedefinitionmayexecuteconc

C++11 for_each 和 lambdas 优化

我正在测试以下代码:#include#include#include#includeintmain(intargc,char*argv[]){std::vectorv(10000000);clock_tthen=clock();if(argc我正在使用g++4.6编译它,没有任何优化标志,这是我得到的:[javadyan@myhostexperiments]$./a.out260000[javadyan@myhostexperiments]$./a.outaaa330000[javadyan@myhostexperiments]$使用-O1优化会产生以下(意料之中的)结果:[javad

c++ - 如何在 Metro 应用程序中打印到 C++ 中的 Visual Studio 11 控制台/日志?

我正在尝试弄清楚如何在VisualStudio11C++Metro应用程序中打印到调试器控制台/日志。我尝试了多种方法来打印到C++Metro应用程序中的日志(cout、Windows.System.Diagnostics、MSDN和Metro开发站点上的文档)。如何打印到控制台? 最佳答案 OutputDebugString:Sendsastringtothedebuggerfordisplay.某些WindowsAPI可从Metro风格应用程序使用,包括此功能。您可以从Metro风格应用程序中找到其他与诊断相关的WindowsA

c++11 统一初始化不适用于 clang++

我从thiswikipediapage复制了以下示例:structBasicStruct{intx;doubley;};structAltStruct{AltStruct(intx,doubley):x_{x},y_{y}{}private:intx_;doubley_;};BasicStructvar1{5,3.2};AltStructvar2{2,4.3};intmain(intargc,charconst*argv[]){return0;}然后我尝试用编译它clang++-Wall-std=c++11test.cpp但是我得到这个错误:test.cpp:17:11:error:n