草庐IT

variable-declaration

全部标签

c++ - 如何将 "point of declaration"解释为 "const int i=2; { int i[i]; }"- C++ 标准中的示例?

我正在研究C++标准以了解操作顺序、表达式、语句和副作用。一个相关的问题是名称的“声明点”。在C++11标准的§3.3.2.1节中,该标准规定:Thepointofdeclarationforanameisimmediatelyafteritscompletedeclarator(Clause8)andbeforeitsinitializer(ifany)...以下段落添加了带有示例的注释:Note:anamefromanouterscoperemainsvisibleuptothepointofdeclarationofthenamethathidesit.……举个例子constin

C++ 错误 : ‘_mm_sin_ps’ was not declared in this scope

我正在尝试对将函数应用于数组的不同方法进行基准测试。为什么是https://software.intel.com/sites/landingpage/IntrinsicsGuide/#expand=3260,2124,4779,4779&cats=Trigonometry&text=_sin_mm_sin_ps在我的范围内未知,但_mm_sqrt_ps是?我如何让它为人所知?并编译无误。#include#include#include#include#include#include#include"immintrin.h"#includeintmain(){std::coutdis(-

c++ - 什么时候可以在没有谓词的情况下使用 std::condition_variable?

如果std::condition_variable可以由于虚假唤醒而发出信号(并且我们不能确定我们需要的条件是否真的得到满足),为什么C++标准库提供重载没有谓词的wait()方法?什么场景可以使用这种行为? 最佳答案 假设一个复杂条件:A||B。当条件的任何部分为真时,应执行适当的操作,actionA或actionB。使用predicate版本,代码如下:cond.wait(lock,[]{return(A||B);});if(A){actionA();}else{actionB();}但如果使用非谓词等待,代码可能更快:whil

c++ - 错误 : ambiguates old declaration ‘double round(double)’

/usr/include/i386-linux-gnu/bits/mathcalls.h:311:1:error:ambiguatesolddeclaration‘doubleround(double)’g.cpp:Infunction‘intround(double)’:g.cpp:14:24:error:newdeclaration‘intround(double)’/usr/include/i386-linux-gnu/bits/mathcalls.h:311:1:error:ambiguatesolddeclaration‘doubleround(double)’#includ

c++ - 为什么 std::condition_variable 的通知和等待函数都需要一个锁定的互斥量

在我对理解std::contion_variable的永无止境的探索中,我遇到了以下问题。在thispage它说了以下内容:voidprint_id(intid){std::unique_locklck(mtx);while(!ready)cv.wait(lck);//...std::cout然后它说:voidgo(){std::unique_locklck(mtx);ready=true;cv.notify_all();}据我所知,这两个函数都将在std::unqique_lock行停止。直到获得唯一锁。也就是说,没有其他线程有锁。假设print_id函数首先执行。将获取唯一锁,函数

c++ - 在 C++ 中 : why does a constructor get called when an array of objects is declared?

MyClassmc2[]={MyClass(),MyClass()};//thiscallstheconstructortwiceMyClassmc1[4];//thiscallstheconstructor4times.Why?所以,我的问题是:为什么没有初始化的对象数组声明会导致调用默认构造函数? 最佳答案 在C++中,大小为4的MyClass数组是四个实际对象。它有点像包含该类型的四个成员的结构,当然您可以使用不同的语法访问这些成员,并且存在其他技术差异。因此,定义该数组导致构建4个对象的原因(并且在大致相同的情况下)与定义该

C++ 错误 : definition of implicitly-declared

我正在用C++编写这个链表程序当我测试程序时,我得到了错误linkedlist.cpp:5:24:error:definitionofimplicitly-declared'constexprLinkedList::LinkedList()'LinkedList::LinkedList(){这是代码链表.h文件:#include"node.h"usingnamespacestd;classLinkedList{Node*head=nullptr;intlength=0;public:voidadd(int);boolremove(int);intfind(int);intcount(i

c++ - 变长数组 : How to create a buffer with variable size in C++

我目前正在编写一个移动平均线类。目标是在创建Running_Average类的新对象时能够将缓冲区大小指定为构造函数的一部分。#include#include"Complex.h"#include#include#include#includeusingnamespacestd;classRunning_Average{public:doublesum=0;doubleaverage=0;inti;doubleAverage(void);//MemberfunctionsdeclarationvoidAddSample(double);Running_Average(int);};Ru

c++ - C++模板函数中,依赖函数调用为什么报 "not declared"错误?

在C++模板函数foo()中,调用::bar(TT*)在gcc4.4.3下会出现以下错误:g++-ohello.o-c-ghello.cpphello.cpp:Infunction'voidfoo(std::vector>&)':hello.cpp:8:error:'::bar'hasnotbeendeclared这是有问题的代码://hello.cpp#includetemplatevoidfoo(std::vector&vec){TT*tt;::bar(tt);vec.push_back(tt);}classBlah{};voidbar(Blah*&){}intmain(intar

C++ : Initializing base class constant static variable with different value in derived class?

我有一个带有常量静态变量a的基类A。我需要类B的实例对静态变量a具有不同的值。这怎么能实现,最好是静态初始化?classA{public:staticconstinta;};constintA::a=1;classB:publicA{//???//Howtoset*a*toavaluespecifictoinstancesofclassB?}; 最佳答案 你不能。所有派生类共享一个静态变量实例。 关于C++:Initializingbaseclassconstantstaticvaria