草庐IT

initial_value

全部标签

c++ - 什么时候允许推导 initializer_list?

我知道对于什么时候可以推导出initializer_list有一些特殊的规则,但是直到发现以下内容之前,我不知何故认为这从来都不是。何时可以推导或省略initializer_list的规则是什么?以下示例似乎不合逻辑,感觉几乎像是语言缺陷?#includevoidtest(){boolreverse=true;constautoascend={1,2,3};//OK:seemstodeducetostd::initializer_list//constautoa_or_d_AUTO=reverse?{3,2,1}:{1,2,3};//notok,why?constautoi=rever

c++ - "static initialization"到底是什么意思?

我一直在阅读有关C++11中的POD的文章,我读过的几个地方都提到了支持静态初始化的POD。例如:OnStackOverflow:TheideaofaPODistocapturebasicallytwodistinctproperties:1.Itsupportsstaticinitialization,and2.CompilingaPODinC++givesyouthesamememorylayoutasastructcompiledinC.(只有粗体部分是相关的)OnWikipedia:Atypethatistrivialcanbestaticallyinitialized.显然我

c++ - 在 C++ 中,如何在不使用 new 且不单独声明单个元素的情况下创建 `std::initializer_list<base *>`?

在C++中,您可以在文件范围内声明一个数组:staticfooa[]={foo(),foo(),foo()};各个foo对象具有静态存储(即它们不在运行时分配)。如果我有一个由两个或更多派生类继承的基类,则以下内容可以编译但由于切片而无法按预期工作:staticbasea[]={derived1(),derived2()};这样的事情不应该导致切片发生:staticderived1d1;staticderived2d2;staticbase*a[]={&d1,&d2};我的问题是:如何在不声明d1的情况下做同样的事情?和d2与a分开,同时为单个(指向的)元素保留静态存储?以下给出了“获

c++ - const_reference_type 不编译但 const value_type& 编译

#includetemplatestructref_exp{typedefTvalue_type;typedefvalue_type&reference_type;typedefconstreference_typeconst_reference_type;ref_exp(value_typedata):_data(data){}const_reference_typedata()const{return_data;}private:value_type_data;};intmain(){ref_expexp1(2);std::cout上面的代码无法编译ref.cpp:Inmember

c++ - 错误 "expected initializer before ' 使用'"c++

关闭。这个问题是notreproducibleorwascausedbytypos.它目前不接受答案。这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这个问题的解决方式不太可能帮助future的读者。关闭8年前。Improvethisquestion我是新来的,一般来说也是编程方面的新手。我正在尝试运行这段代码:#include#include#include"main.h"usingnamespacestd;intmain(){shortarr_size()floattemp;pointpoint_arr[99];ifstreammy_fil

c++ - 在 switch case 语句中,它表示 "duplicate case value"作为错误出现。有人知道为什么吗?

我正在编写剪刀石头布程序,但这次计算机有一半的时间选择石头,三分之一的时间选择剪刀,只有六分之一的时间选择布。我这样做的方法是列举了六个可能的计算机选择值:enumchoicec{rock1,rock2,rock3,scissors1,scissors2,paper};choiceccomputer;但是,在计算机做出选择之后,我必须将这些枚举值转换为石头、布或剪刀。我使用switch-case语句完成了此操作:switch(computer){caserock1||rock2||rock3:c=1;break;casescissors1||scissors2://ERROR!c=3;

c++ - 表达式 "variable, variable = value;"

我一直在查看一些MFC代码,我遇到了这个表达式。它在OnInitDialog()函数中,看起来不像是特定于MFC的。变量有一些名字,值为0。intvolatilesomething,somethingElse;//thiswasglobalsomething,somethingElse=0;//thiswasinsidethefunction这在C++中有意义吗?我知道逗号运算符是如何工作的,尽管在像这里这样的自由形式中它应该是分隔表达式。变量名也是表达式吗?这段代码确实可以编译,那么它是如何工作的呢? 最佳答案 这可能是程序中的一

java - Spring Boot 测试中的@Value "Could not resolve placeholder"

我想对Spring-boot进行Junit测试,如下所示:@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes={ApplicationTest.class})publicclassTestOnSpring{@Value("${app.name}")privateStringappName;@TestpublicvoidtestValue(){System.out.println(appName);}}和这样的ApplicationTest.java@ComponentScan("org.nerve.j

java - Spring Boot 测试中的@Value "Could not resolve placeholder"

我想对Spring-boot进行Junit测试,如下所示:@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes={ApplicationTest.class})publicclassTestOnSpring{@Value("${app.name}")privateStringappName;@TestpublicvoidtestValue(){System.out.println(appName);}}和这样的ApplicationTest.java@ComponentScan("org.nerve.j

C++ 错误 : expected initializer before ‘&’ token

以下是两年前在suse10.1Linux机器上编译的C++代码。#ifndefDATA_H#defineDATA_H#include#includeinlinedoublesqr(doublex){returnx*x;}enumDirection{X,Y,Z};inlineDirectionnext(constDirectiond){switch(d){caseX:returnY;caseY:returnZ;caseZ:returnX;}}inlineostream&operator现在,我尝试在Ubuntu9.10上编译它,但出现错误:data.h:20:error:expected