草庐IT

Declaration

全部标签

c++ - 在使用 Clang 编译 CRTP Singleton 时,如何解决声称缺少 "explicit instantiation declaration"的问题?

我们正在使用curiouslyrecurringtemplatepattern实现单例。但是,在最近的Clang版本中,我们收到了-Wundefined-var-template警告。建议的修复方法是添加“显式实例化声明”。我试图这样做,但后来在定义单例模板类成员变量的编译单元中出现有关“实例化后的显式特化”的错误。解决此警告突出显示的问题的适当构造是什么?简化详细信息(已删除大部分逻辑,以制作MCVE):单例基础.hh:templateclassSingletonBase{public:staticT*get_instance(){if(!instance_){instance_=T

c++ - 未命名的命名空间与全局声明

使用无名命名空间和全局声明有什么区别?使用这两个有什么特定的上下文吗?我们可以访问外部源文件中未命名的命名空间组件吗? 最佳答案 未命名命名空间的要点是在翻译单元(=源文件)内提供一个唯一的命名空间,而不需要显式前缀。这允许您保证您的全局名称不会与其他翻译单元中的其他相同的全局名称冲突。例如://file1.cppnamespace{voidfoo(){/*...*/}}#include"bar.h"intdo_stuff(){foo();bar();return5;}//file2.cppnamespace{voidfoo(){/

C++ - 函数范围内的函数声明?

我正在浏览C++11standarddraft不久前遇到这个(在§8.3.6,第204页):voidg(int=0,...);//OK,ellipsisisnotaparametersoitcanfollow//aparameterwithadefaultargumentvoidf(int,int);voidf(int,int=7);voidh(){f(3);//OK,callsf(3,7)voidf(int=1,int);//error:doesnotusedefault//fromsurroundingscope}voidm(){voidf(int,int);//hasnodefa

c++ - 是否可以使用 'using' 来声明对 3 个整数类型别名的引用?

我得到了一个练习,我需要为“对3个整数的引用”使用类型别名。尽管我使用typedef获得了成功,但我无法通过c++11引入的复制它。代码:typedefint(&int_ref)[3];\\成功使用int_ref2=(int&)[3];\\错误我是否应该只使用类似...使用int_ref2=int[3];int_ref2&iruvar... 最佳答案 比较这两个声明typedefint(&int_ref)[3];\\successusingint_ref2=(int&)[3];\\error如您所见,存在差异:在第二个声明中,类型说

c++ - 为什么C++标准中的declaration-seq是这样写的?

declaration-seq:declarationdeclaration-seqdeclaration不是这样的:declaration-seq:declarationdeclarationdeclaration-seq这两个定义可以互换吗?它们有什么区别? 最佳答案 这是C++的C遗产的遗迹。C语法(几乎)是LALR(1),因此使用leftrecursion越多越好。C++语法甚至不再是模糊的LALR,但许多规则仍然以LALR解析器更喜欢的形式编写,因为没有理由改变它们——任何强大到足以处理C++的解析器算法都不关心哪种类型的

c++ - 不合格的名称查找 : Why local declaration hides declaration from using directive

考虑这段代码:namespaceA{inti=24;}namespaceB{usingnamespaceA;inti=11;intk=i;//findsB::i,noambiguity}和basic.lookup.unqual.2:§6.4.1Unqualifiednamelookup[basic.lookup.unqual]Thedeclarationsfromthenamespacenominatedbyausing-directivebecomevisibleinanamespaceenclosingtheusing-directive;see[namespace.udir].F

c++ - Clang VS VC++ :"error: declaration of ' T'阴影模板参数”

我正在尝试为theclassiccopy&swapidiom编译以下代码在我的Mac上使用clang3.3templateclassnode{private:node*left;node*right;Tvalue;public:friendvoidswap(node&,node&);//otherstuff}然而链接器却报错了。我现在明白我应该将函数声明为模板。但是,如果我按照建议的样式进行操作,则会发生错误here来自MSDN:templateclassArray{T*array;intsize;public:template//...templatefriendArray*comb

c++ - 难以理解 C++ 指针语法

我无法理解我在面试中遇到的这段代码声明。int(*(*ptr[3])(char*))[2];我试过查看一个IDE,但我所拥有的只是它是一个数据类型的数组int(*(*[3])(char*))我无法理解这一点。 最佳答案 或许您可以一次分解一个,以便更好地理解语法。首先从一个没有数组符号的简单定义开始int(*(*ptr)(char*));所以ptr是一个函数指针,它接受一个char指针作为参数并返回一个指向int的指针。现在将其扩展为数组表示法int(*(*ptr[3])(char*))[2];这意味着您有一个函数指针数组,每个函数

c++ - 重新声明错误

我明白了声明和定义的区别当我遇到疑问时,我正在练习一些问题,下面的代码要求我列出片段中的错误。f(inta,intb){inta;a=20;returna;}为什么这会导致a的重新声明错误?它不应该给出a的多个定义,因为在:f(inta,intb)—这里a定义正确吗?并且在函数体中,又定义了inta?那么为什么不是多重定义错误呢? 最佳答案 定义始终是声明。不同之处在于,定义还会为您声明的任何内容提供一些值。顺便说一下,在您的示例中,它只是一个重新声明错误:f(inta,/*Definesa*/intb){inta;/*Declar

c++ - 为什么编译器说 : 'enable_if' cannot be used to disable this declaration

templateusingEnable_if=typenamestd::enable_if::type;classDegree;templateconstexprinlineboolIs_Degree(){returnstd::is_base_of::value;}classDegree{public:std::size_tinDeg=0;};templateclassVertex:publicSatellite{public:explicitVertex(intnum):n(num){}private:std::size_tn;};templateclassEdge{public:/