标签: specifier

我应该在C++中使用异常说明符吗?

在C++中,您可以通过使用异常说明符指定函数可能会也可能不会抛出异常.例如:

void foo() throw(); // guaranteed not to throw an exception
void bar() throw(int); // may throw an exception of type int
void baz() throw(...); // may throw an exception of some unspecified type
Run Code Online (Sandbox Code Playgroud)

由于以下因素,我对实际使用它们表示怀疑:

  1. 编译器并没有以任何严格的方式真正强制执行异常说明符,因此效益并不高.理想情况下,您希望得到编译错误.
  2. 如果函数违反了异常说明符,我认为标准行为是终止程序.
  3. 在VS.Net中,它将throw(X)视为throw(...),因此遵守标准并不强.

你认为应该使用异常说明符吗?
请回答"是"或"否"并提供一些理由来证明您的答案.

c++ exception function throw specifier

119
推荐指数
5
解决办法
3万
查看次数

C++ 11标准参考使用类型说明符中允许的类型定义?

在C++ 11中,类型说明符包括类说明符枚举说明符.(又名类定义和枚举定义)

根据语法/语法 - 类型说明符可以出现在语言的几个地方,但不是所有那些地方都允许使用类说明符和枚举说明符.

例如:

struct C{} c;
// ok: types may be defined in the specifiers of a simple declaration

void f(struct S{});
// error: types may not be defined in parameter types

constexpr auto i = sizeof(enum E{});
// error: types may not be defined in ‘sizeof’ expressions
Run Code Online (Sandbox Code Playgroud)

标准中的哪些部分将类型说明符的这些用法划分为可以定义类型和不定义类型的那些?例如,在sizeof表达式中可能没有定义类型的规则在哪里?

c++ language-lawyer specifier type-declaration c++11

10
推荐指数
1
解决办法
628
查看次数

我们应该提供一个没有抛出说明符的析构函数吗?

namespace QuantLib {

    //! Base error class
    class Error : public std::exception {
      public:
        /*! The explicit use of this constructor is not advised.
            Use the QL_FAIL macro instead.
        */
        Error(const std::string& file,
              long line,
              const std::string& functionName,
              const std::string& message = "");
        /*! the automatically generated destructor would
            not have the throw specifier.
        */
        ~Error() throw() {}
        //! returns the error message.
        const char* what() const throw ();
      private:
        boost::shared_ptr<std::string> message_;
    };

}
Run Code Online (Sandbox Code Playgroud)

正如您在注释中看到的那样,类的析构函数Error显式提供了一个带有no-throw说明符的空实现.

问题:这有必要吗?或者这是一个很好的做法,比较让编译器生成隐式析构函数?

c++ destructor exception throw specifier

8
推荐指数
2
解决办法
2838
查看次数

错误:C++ 要求所有声明都有类型说明符

我是 C++ 新手,我一直在读这本书。我读了几章,想到了自己的想法。我尝试编译下面的代码,但出现以下错误:

||=== 构建:密码调试(编译器:GNU GCC 编译器)===| /Users/Administrator/Desktop/AppCreations/C++/Password/Password/main.cpp|5|错误:C++ 需要所有声明的类型说明符| ||=== 构建失败:1 个错误,0 个警告(0 分钟,2 秒)===|。

我不明白代码有什么问题,有人可以解释一下有什么问题以及如何修复它吗?我读了其他帖子,但我无法理解。

谢谢。

#include <iostream>

using namespace std;

main()
{
    string password;
    cin >> password;
    if (password == "Lieutenant") {
        cout << "Correct!" << endl;
    } else {
        cout << "Wrong!" << endl;
    }

}
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors codeblocks specifier

6
推荐指数
2
解决办法
6万
查看次数

C++什么类型的参数类型名称可以与带有类型说明符的参数名称相同?

抱歉,这个问题需要一些探索.我正在修复doxygen解析一些C++代码的疏忽,我遇到了一个不寻常的角落案例,doxygen没有考虑到.我有一个修复,但我想让它更一般,所以我需要一些解释.

为了说明doxygen失败的情况,我将定义一个涉及"辛普森一家"的人为例子(因为这对于这些类型的问题似乎很受欢迎).假设我们有以下枚举:

enum simpson { HOMER, MARGE, BART, LISA, MAGGIE };
Run Code Online (Sandbox Code Playgroud)

现在我们要将枚举值传递给一个方法(自然是Simpsons类),如下所示:

const char* voicedBy(simpson simpson)
{
    switch (simpson) {
        case HOMER:
            return "Dan Castellaneta";
        case MARGE:
            return "Julie Kavner";
        case BART:
            return "Nancy Cartwright";
        case LISA:
            return "Yeardley Smith";
        case MAGGIE:
            return "*suck* *suck*";
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,这会产生编译器错误,因为不允许枚举类型'simpson'与参数名称'simpson'相同(与C#不同).但是,C++有一个答案.您将enum关键字放在类型名称前面,如下所示:

const char* voicedBy(enum simpson simpson)
Run Code Online (Sandbox Code Playgroud)

现在代码将编译并运行.不幸的是,doxygen没有考虑到这种情况,所以它将整个字符串"enum simpson simpson"视为没有参数名称的参数类型.我已经提出了一些代码来修复doxygen,就像上面的枚举一样.

我的问题是,这种技巧有哪些其他类型?struct?,union?,typedef?,其他?就此而言,与参数名称'概念同名的方法参数的'类型说明符'是否具有名称,以便我可以获得更多详细信息?

c++ parameters enums specifier

5
推荐指数
1
解决办法
1491
查看次数

Swiftui 指定双格式

Text("Värde \(Double(calc2, specifier: "%.2f").rounded())") 
Run Code Online (Sandbox Code Playgroud)

//错误

我正在转换一个Slider使用 double 类型的值,但我无法格式化指定它?我收到一个错误,xcode 告诉我使用信号(?)。我也试过把它放进一个Int

什么是正确的举动?

format xcode specifier swift swiftui

5
推荐指数
2
解决办法
3139
查看次数

Python:想要使用字符串作为切片说明符

假设我有一个S字符串 的变量"1:3"(或者就此而言"1",或者"1:"或者":3")我希望将它用作列表 上的切片说明符L.你不能简单地做,L[S]因为切片所需的args是"int:int".

现在,我目前有一些丑陋的代码解析S它的两个组成部分int并处理所有边缘情况(其中4个)以提出正确的切片访问,但这只是简单的丑陋和unpythonic.

我如何优雅地使用字符串S并将其用作切片说明符?

python string slice specifier

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

我如何在C中编写一个新的自己的格式说明符?

是否可以在C中编写新的格式说明符?例如,假设%g是格式说明符,它以无符号整数等价物打印ABCD格式的ip地址.

int ip_addr = Some integer

printf("ip address = %g", ip_addr);
Run Code Online (Sandbox Code Playgroud)

输出:以ABCD格式打印ip地址

c format specifier

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

错误C3646:'closure':未知的覆盖说明符

我收到以下错误:

error C3646: 'closure' : unknown override specifier
Run Code Online (Sandbox Code Playgroud)

代码:

void BaseOperator::mousebutton_cb(EventObject* sender, EventArgs* calldata, void* closure)
{
    xd3D::Operation::Operator::BaseOperator* operator = (xd3D::Operation::Operator::BaseOperator*)closure;
    MouseButtonEventArgs* e = (MouseButtonEventArgs*)calldata;
    if (e->Status == Down)
        operator->OnMouseButtonDown(e);
    else
        operator->OnMouseButtonUp(e);
}
Run Code Online (Sandbox Code Playgroud)

你知道为什么我有这个错误吗?

c++ overriding specifier

3
推荐指数
1
解决办法
9537
查看次数

缺少类型说明符 - 在C++中假设为Int

这些第二行是抛出这个错误,但我不确定为什么.

std::vector<std::string> weapons(100);
weapons[3] = "Rusty dagger";
Run Code Online (Sandbox Code Playgroud)

-

这是我的整个文件:

//global variables
#ifndef _GLOBAL_
#define _GLOBAL_

#include <vector>
#include <iostream>
#include <string>

//prototypes
void NPCTalk(std::string const& speaker,std::vector<std::string> const& text);
void wait(double seconds);
void regionChange(int amount);
int getPositionInStringVector(std::vector<std::string> const& vec,std::string value);

//variables
std::vector<std::string> weapons(100);
weapons[3] = "Rusty dagger";

//defines
#define RegionChange 3

#endif //__GLOBAL__
Run Code Online (Sandbox Code Playgroud)

c++ types specifier

3
推荐指数
1
解决办法
7299
查看次数