草庐IT

c++ - 为什么 try..catch block 需要大括号?

虽然在if...else等其他语句中,如果block中只有一条指令,您可以避免使用大括号,但您不能使用try...catchblock来做到这一点:编译器不会购买它。例如:trydo_something_risky();catch(...)std::cerr使用上面的代码,g++只是说它在do_something_risky()之前需要一个“{”。为什么try...catch和if...else之间的这种行为差异?谢谢! 最佳答案 直接来自C++规范:try-block:trycompound-statementhandler-se

c++ - 什么时候可以在初始化列表中省略外括号?

我在VC2010编译下面的代码时出现错误C2078。structA{intfoo;doublebar;};std::arraya1=//errorC2078:toomanyinitializers{{0,0.1},{2,3.4}};//OKstd::arraya2={0.1,2.3};我发现a1的正确语法是std::arraya1={{{0,0.1},{2,3.4}}};问题是:为什么a1需要额外的大括号,而a2却不需要?更新这个问题似乎并不特定于std::array。一些例子:structB{intfoo[2];};//OKBmeow1={1,2};Bbark1={{1,2}};st

c++ - 什么时候可以在初始化列表中省略外括号?

我在VC2010编译下面的代码时出现错误C2078。structA{intfoo;doublebar;};std::arraya1=//errorC2078:toomanyinitializers{{0,0.1},{2,3.4}};//OKstd::arraya2={0.1,2.3};我发现a1的正确语法是std::arraya1={{{0,0.1},{2,3.4}}};问题是:为什么a1需要额外的大括号,而a2却不需要?更新这个问题似乎并不特定于std::array。一些例子:structB{intfoo[2];};//OKBmeow1={1,2};Bbark1={{1,2}};st

c++ - 为什么 Clang 和 VS2013 接受移动大括号初始化的默认参数,但不接受 GCC 4.8 或 4.9?

如标题所示,我有一个简短的演示程序,可以使用所有这些编译器进行编译,但在使用gcc4.8和gcc4.9编译后运行时核心转储:有什么想法吗?#includestructFoo:std::unordered_map{usingstd::unordered_map::unordered_map;//~Foo()=default;//addingthisallowsittowork};structBar{Bar(Foof={}):_f(std::move(f)){}//usinganyofthefollowingconstructorsfixestheproblem://Bar(Foof=Fo

c++ - 为什么 Clang 和 VS2013 接受移动大括号初始化的默认参数,但不接受 GCC 4.8 或 4.9?

如标题所示,我有一个简短的演示程序,可以使用所有这些编译器进行编译,但在使用gcc4.8和gcc4.9编译后运行时核心转储:有什么想法吗?#includestructFoo:std::unordered_map{usingstd::unordered_map::unordered_map;//~Foo()=default;//addingthisallowsittowork};structBar{Bar(Foof={}):_f(std::move(f)){}//usinganyofthefollowingconstructorsfixestheproblem://Bar(Foof=Fo

c++ - 聚合初始化的 C++17 扩展是否使大括号初始化变得危险?

似乎普遍认为braceinitializationshouldbepreferred超过其他形式的初始化,但是自从引入C++17extensiontoaggregateinitialization似乎存在意外转换的风险。考虑以下代码:structB{inti;};structD:B{charj;};structE:B{floatk;};voidf(constD&d){Ee1=d;//errorC2440:'initializing':cannotconvertfrom'D'to'E'Ee2(d);//errorC2440:'initializing':cannotconvertfrom

c++ - 聚合初始化的 C++17 扩展是否使大括号初始化变得危险?

似乎普遍认为braceinitializationshouldbepreferred超过其他形式的初始化,但是自从引入C++17extensiontoaggregateinitialization似乎存在意外转换的风险。考虑以下代码:structB{inti;};structD:B{charj;};structE:B{floatk;};voidf(constD&d){Ee1=d;//errorC2440:'initializing':cannotconvertfrom'D'to'E'Ee2(d);//errorC2440:'initializing':cannotconvertfrom

c++ - decltype 和括号

我不明白FCD第148页示例的最后一行(第7.6.1.2/4节):constint&&foo();inti;structA{doublex;};constA*a=newA();decltype(foo())x1=i;//typeisconstint&&decltype(i)x2;//typeisintdecltype(a->x)x3;//typeisdoubledecltype((a->x))x4=x3;//typeisconstdouble&为什么括号在这里有所不同?不应该像上一行那样简单地double吗? 最佳答案 上面那个例子

c++ - decltype 和括号

我不明白FCD第148页示例的最后一行(第7.6.1.2/4节):constint&&foo();inti;structA{doublex;};constA*a=newA();decltype(foo())x1=i;//typeisconstint&&decltype(i)x2;//typeisintdecltype(a->x)x3;//typeisdoubledecltype((a->x))x4=x3;//typeisconstdouble&为什么括号在这里有所不同?不应该像上一行那样简单地double吗? 最佳答案 上面那个例子

c++ - 重载括号运算符 [] 以获取和设置

我有以下类(class):classrisc{//singletonprotected:staticunsignedlongregisters[8];public:unsignedlongoperator[](inti){returnregisters[i];}};如您所见,我已经为“获取”实现了方括号运算符。现在我想实现它进行设置,即:risc[1]=2.怎么做? 最佳答案 试试这个:classrisc{//singletonprotected:staticunsignedlongregisters[8];public:unsig