草庐IT

c++ - 使用引用推导模板包中的冲突类型

我正在开发一个具有以下结构的程序:#include#includevoidfun(conststd::string&text,inta,intb){//(1)std::coutvoidexecute(void(*fun)(Args...),Args...args){fun(args...);}voidinit(conststd::string&text,inta,intb){execute(fun,text,a,b);}intmain(){init("Fun:",1,2);return0;}我收到了错误信息.code.tio.cpp:14:2:error:nomatchingfunct

c++ - 使用 'g++' 结果为 "warning: will never be executed"

我继承了一个C++项目。我在RHELbuild5.5与GCC4.1.2通过makefile。该项目很大(数百个文件),总的来说代码还不错。然而,在编译过程中,我经常收到一个GCC警告,上面写着(prefix"/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2"):/bits/allocator.h:Inconstructor‘std::allocator::allocator()[with_Tp=char]’:/bits/allocator.h:97:warning:willneverbeexecuted

跨平台代码中的c++模板问题

我在Linux上编译这段代码时遇到了一些麻烦,但它在Windows上运行得很好。Windows编译器:VisualStudio2005Linux编译器:gcc版本3.4.320041212(RedHat3.4.3-9.EL4)classDoSomething{public:templateboolExecute(){//dosomethinghere}};templateTypeSwitch(intDataTypeCode,Operator&Op){switch(DataTypeCode){case1:returnOp.Execute();case2:returnOp.Execute(

c++ - (C++ 线程): Creating worker threads that will be listening to jobs and executing them concurrently when wanted

假设我们有两个worker。每个worker都有一个0和1的id。还假设我们一直有工作到达,每个工作也有一个标识符0或1指定哪个worker必须做这个工作。我想创建2个线程,它们最初是锁定的,然后当两个作业到达时,解锁它们,每个线程都完成它们的工作,然后再次锁定它们,直到其他作业到达。我有以下代码:#include#include#includeusingnamespacestd;structjob{threadjobThread;mutexjobMutex;};jobjobs[2];voidexecuteJob(intworker){while(true){jobs[worker].

c++ - 为什么 C++ 标准 1.9/5 谈论 "possible execution sequences"?

根据C++03标准1.9/5Aconformingimplementationexecutingawell-formedprogramshallproducethesameobservablebehaviorasoneofthepossibleexecutionsequencesofthecorrespondinginstanceoftheabstractmachinewiththesameprogramandthesameinput.我不明白“作为其中一个”部分。如果我有一个特定的程序和一个特定的输入,并且我的程序不包含未定义的行为,为什么可观察到的行为会有所不同?“一种可能的执行顺

c++ - 追踪器 : error TRK0002: Failed to execute command

我在尝试在作为服务运行的buildAgent上构建我的项目时遇到此错误,有人有解决方案吗?TRACKER:errorTRK0002:Failedtoexecutecommand:""C:\ProgramFiles(x86)\MicrosoftVisualStudio14.0\VC\bin\amd64\CL.exe"@C:\BuildAgent\temp\buildTmp\tmpfde187c5fd8a42299ab4d18e25e0c9fe.rsp".Theoperationidentifierisnotvalid.在命令行中构建项目时(使用“_IsNativeEnvironment”

c++ - 给定范围内的完美正方形 : abnormal execution of loops

程序编号1:在给定的a和b范围内,其中ab,我想找出一个数字是否是一个完美正方形,如果是,则打印其根。因此,我编写了如下代码:#include#include#include#includeusingnamespacestd;floatsquaredroot(intn){floatlow=0.0,mid;floathigh=(float)n+1;while((high-low)>0.00001){mid=(low+high)/2;if(mid*mid>a>>b;floatroo=0.0;for(i=a;i对于给定的输入15,输出应该是2。但是,上面的程序没有打印任何值。然而,当我尝试使

c++ - g++ : can't link with a main executable file

我正在开发一个使用统计攻击来破解wepkey的应用程序。当我用我的makefile编译时(如上)我得到这个错误:ld:can'tlinkwithamainexecutablefile'execStatAttack'forarchitecturex86_64clang:error:linkercommandfailedwithexitcode1(use-vtoseeinvocation)make:*[statAttack]Error1我的项目包含那些文件:statAttack.cpp:包含主要功能,使用上面的文件rc4.h+rc4.cpp:具有那些功能#include#include#i

c++ - 打印 std::this_thread::get_id() 给出 "thread::id of a non-executing thread"?

这曾经工作得很好(然后外星人一定黑了我的电脑):#include#includeintmain(){std::cout现在它打印thread::idofanon-executingthread。ideone.com打印了一些ID,但有趣的是是什么导致了我平台上的这种行为。$uname-aLinuxxxx3.13.0-77-generic#121-UbuntuSMPWedJan2010:50:42UTC2016x86_64x86_64x86_64GNU/Linux有什么想法吗?编辑:嗯..当我添加std::cout两行打印相同的ID,但是当我删除它时,结果仍然相同-“非执行线程”。

c++ - 在这种情况下,有人可以解释 "reference"和 "pointer"之间的区别吗?

当我读到litbanswertothisquestion,我了解到通过引用传递数组可以让我们获得它的大小。我只是玩了一点代码,并尝试通过引用传递一个“函数”,令人惊讶的是(至少对我而言),这段代码编译:voidexecute(void(&func)())//funcispassedbyreference!{func();}上一个函数和这个函数有什么区别吗:voidexecute(void(*func)())//funcispassedbypointer!{func();}我用VC2008试过了,在每种情况下它都会产生不同的输出。奇怪的是编译器在函数指针的情况下更好地优化了代码:void