相关疑难解决方法(0)

函数try catch语法和main

一个鲜为人知但几乎从未使用过的C++特性的声明是:

void foo();
Run Code Online (Sandbox Code Playgroud)

一种可能的法律定义可能是:

void foo() try {
  throw 42;
}
catch(...) {
}
Run Code Online (Sandbox Code Playgroud)

这里包含的整个函数实现在try/ catchpair中,这似乎与允许相似.

那是合法的int main()吗?例如:

int main() try {
  throw 42;
}
catch(...) {
}
Run Code Online (Sandbox Code Playgroud)

main,n3290§3.6.1 的规则主要讨论它应该采取什么样的论据以及它返回什么 - 它们似乎并没有明确地禁止它,因为它们与你可能想要尝试的各种其他奇怪事物(例如联系)一样.

这是合法的,定义明确吗?

c++ program-entry-point exception-handling language-lawyer

16
推荐指数
1
解决办法
3495
查看次数

这个语法是什么意思?

我刚才看到SO问题中的特殊语法.

class B{
    A a;
    public:
        B() try : a() {} catch(string& s) { cout << &s << " " << s << endl; };
};
Run Code Online (Sandbox Code Playgroud)

这个函数外面的try-catch-block是什么意思?

c++ syntax

9
推荐指数
1
解决办法
379
查看次数

主括号中的括号和返回值

我读了这段代码(由Bjarne Stroustrup撰写).我很困惑... main函数体不在{},函数不返回值(as int).它有效......为什么?

#include "std_lib_facilities.h" 

int main()
try
{
    cout<< "please enter two floating-point values separated by an operator\n The operator can be + - * or / : ";
    double val1 = 0;
    double val2 = 0;
    char op = 0;
    while (cin>>val1>>op>>val2) {   // read number operation number
        string oper;
        double result;
        switch (op) {
        case '+':
            oper = "sum of ";
            result = val1+val2; 
            break;
        case '-':
            oper = "difference …
Run Code Online (Sandbox Code Playgroud)

c++

4
推荐指数
1
解决办法
853
查看次数