小编pab*_*285的帖子

禁用特定宏的 clang-tidy 警告

我有一个实例化全局变量的宏。因此 clang-tidy 将正确发出“使用静态存储持续时间初始化...可能会引发无法捕获的异常”。

\n\n

我可以针对每个宏禁用此警告吗?最好在定义宏的地方内联。

\n\n

也就是说,假设我有:

\n\n
// header.h\n#define UGLY_MACRO(X)  SomeClass X(#X)\n\n// source.cpp\nUGLY_MACRO(SomeName); // clang-tidy complains here\n
Run Code Online (Sandbox Code Playgroud)\n\n

我希望 clang-tidy 停止抱怨这一点。

\n\n

我想尽可能具体。\n我只想为此宏关闭警告。我不想全局关闭警告,那么有人可能会在代码中添加更多此类宏而不会被注意到。 \n另外,我不想添加某些内容(例如 //NOLNT(.. .)) 在每个使用宏的地方,这太麻烦了。

\n\n

或者我是从错误的方向接近这个问题的?我以前曾使用过 pc-lint,这是可能的。

\n

c++ clang-tidy

7
推荐指数
2
解决办法
5675
查看次数

为什么 Clang 不允许“和”作为函数名?

我现在使用 Visual Studio 2017 开发程序已有一段时间了。最近我安装了 Clang Power Tool 扩展以检查我的代码质量。我的程序的一部分包括模拟 cpu 的操作码。我在下面创建了一个精简的示例。

以下示例工作正常:

class C{};

inline void andi(C& s);

int main()
{
    std::cout << "Hello, world!\n";
}
Run Code Online (Sandbox Code Playgroud)

这个没有:

class C{};

inline void and(C& s);

int main()
{
    std::cout << "Hello, world!\n";
}
Run Code Online (Sandbox Code Playgroud)

我在 Clang 3.8.0 上收到这些错误(我在我的程序上使用 9.0.1 版,错误类似):

source_file.cpp:9:18: error: expected ')'
inline void and(C& s);
                 ^
source_file.cpp:9:16: note: to match this '('
inline void and(C& s);
               ^
source_file.cpp:9:13: error: cannot form a reference to 'void'
inline void and(C& s);
            ^ …
Run Code Online (Sandbox Code Playgroud)

c++ clang clangpowertools

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

我可以用constexpr函数声明一个静态数组

// since we can dedfine a static array like this
int a[5] = {0};
// decltype(a[5]) is `int [5]`

// Then how about this way?
constexpr int sum(int a, int b) { return a + b; }
int a, b;
std::cin >> a >> b;
int arr[sum(a, b)] = {0};
Run Code Online (Sandbox Code Playgroud)

它可以成功编译,但是是arr一个静态数组?当我尝试arrtypeid().name()或打印类型时boost::typeindex::type_id_with_cvr,我得到以下错误:

error: cannot create type information for type 'int [(<anonymous> + 1)]' because it involves types of variable size
 std::cout << typeid(decltype(arr)).name() …
Run Code Online (Sandbox Code Playgroud)

c++ constexpr c++11

2
推荐指数
1
解决办法
146
查看次数

标签 统计

c++ ×3

c++11 ×1

clang ×1

clang-tidy ×1

clangpowertools ×1

constexpr ×1