小编iva*_*ult的帖子

当空指针不是所有位为零时,如何正确编写C/C++代码

正如comp.lang.c常见问题所述,有一些架构,其中空指针不是所有位零.所以问题是实际检查以下结构:

void* p = get_some_pointer();
if (!p)
    return;
Run Code Online (Sandbox Code Playgroud)

我是否p与机器相关的空指针进行比较或者我是否p与算术零进行比较?

我应该写

void* p = get_some_pointer();
if (NULL == p)
    return;
Run Code Online (Sandbox Code Playgroud)

相反,为这样的架构做好准备还是仅仅是我的偏执?

c c++ computer-architecture

69
推荐指数
4
解决办法
4800
查看次数

移位运算符的结果类型是什么?

考虑以下清单:

#include <type_traits>
#include <cstdint>

static_assert(std::is_same_v<decltype(31), int32_t>);
static_assert(std::is_same_v<decltype(31u), uint32_t>);

static_assert(std::is_same_v<decltype((signed char)1 << 1), int32_t>);
static_assert(std::is_same_v<decltype((signed char)1 << 1u), int32_t>);
static_assert(std::is_same_v<decltype((unsigned char)1 << 1), int32_t>);
// Signed result for unsigned char
static_assert(std::is_same_v<decltype((unsigned char)1 << 1u), int32_t>);
// But unsigned for uint32_t
static_assert(std::is_same_v<decltype(1u << 1u), uint32_t>);
Run Code Online (Sandbox Code Playgroud)

它可以与 GCC 和 Clang 很好地编译。我很困惑operator<<(uint8_t, uint32_t)。为什么要签署结果?

c++ types bit-shift integer-promotion

22
推荐指数
2
解决办法
2117
查看次数

为什么这个常数表达式不是常数

我有以下C清单:

static const int constant = (0 | ((((1 << 6) - 1) << ((((0 + 8) + 8) + 3) + 7)) & ((1) << ((((0 + 8) + 8) + 3) + 7))) | ((((1 << 7) - 1) << (((0 + 8) + 8) + 3)) & ((0) << (((0 + 8) + 8) + 3))) | ((((1 << 3) - 1) << ((0 + 8) + 8)) & ((0) << ((0 + 8) + 8))) | …
Run Code Online (Sandbox Code Playgroud)

c gcc

13
推荐指数
1
解决办法
255
查看次数

是否可以使用 clang-tidy 忽略标头

我想包含来自外部项目的标头,但 clang-tidy 对此非常不满意,并生成了大量警告列表。为了解决这个问题,我尝试禁用来自此标头的所有诊断。

我试过:

// NOLINTBEGIN
// NOLINTNEXTLINE
#include <bad.hpp> // NOLINT
// NOLINTEND
Run Code Online (Sandbox Code Playgroud)

但不幸的是,这不起作用。

电子邮件线程建议使用-header-filter(HeaderFilterRegex) 选项。

HeaderFilterRegex: '^((?!bad.hpp).)*$'
Run Code Online (Sandbox Code Playgroud)

但这会导致所有标头被忽略,因为 clang tidy使用POSIX 正则表达式语法。这不支持负面展望。

正如这个答案所建议的那样,我还考虑使用线路过滤器,但配置文件没有这样的选项。

有可能吗?

clang-tidy

11
推荐指数
1
解决办法
7969
查看次数

为什么const在这个模板结构中丢失了?

众所周知,以下函数指针具有不同的类型:

void foo_int_ref(int&);
void foo_const_int_ref(const int&);

static_assert(
    !std::is_same<
        decltype(foo_int_ref),
        decltype(foo_const_int_ref)
    >::value,
    "Types should be different");
Run Code Online (Sandbox Code Playgroud)

让我们考虑一下这种概括:

template<typename T>
void foo(T t) {}

template<typename T>
struct ref_vs_const_ref {
    typedef decltype(foo<T>) foo_T;
    typedef decltype(foo<const T>) foo_const_T;
};

using int_ref_vs_const_ref = ref_vs_const_ref<int&>;

static_assert(
    !std::is_same<
        typename int_ref_vs_const_ref::foo_T,
        typename int_ref_vs_const_ref::foo_const_T
    >::value,
    "Types should be different"); // -- it fails
Run Code Online (Sandbox Code Playgroud)

最后一个断言失败了.由于某种原因,const失去了foo_const_T.但为什么?

c++ templates language-lawyer

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

如何将 pre commit 预安装到 docker 的 hooks 中

据我了解文档,每当我将这些行添加到配置中时:

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.1.0
    hooks:
    -   id: trailing-whitespace
Run Code Online (Sandbox Code Playgroud)

它预先承诺从该存储库下载挂钩代码并执行它。是否可以以某种方式将所有钩子预先安装到 Docker 映像中。那么我打电话时pre-commit run没有使用网络吗?

我发现文档的这一部分描述了预提交如何缓存所有存储库。它们存储在~/.cache/pre-commit并且可以通过更新PRE_COMMIT_HOME环境变量来配置。

但是,缓存仅在我这样做时才起作用pre-commit run。但我想预先安装所有内容而不运行检查。是否可以?

pre-commit.com

8
推荐指数
1
解决办法
1万
查看次数

如何并行运行多个Qt Quick测试?

看起来Qt Quick测试要求测试窗口具有焦点,以便发送自己的鼠标和键盘事件.没关系,我只运行一次测试.但是当我尝试运行Qt Quick测试的多个实例时,他们争取窗口焦点,这会导致测试失败.

如何在不关注测试窗口的情况下执行Qt Quick测试?有没有选择强制Qt Quick使用假的鼠标和键盘事件而不是使用真正的窗口系统?如何在没有焦点问题的情况下运行多个Qt Quick测试实例?

c++ qt qt-quick

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

如何在编译时检查函数是否在全局范围内声明

例如,让我有一个标题#include <GL/gl.h>.它包含OpenGL API函数的子集.我需要这样的东西:

static_assert(has_glDrawArraysIndirect::value, "There is no glDrawArraysIndirect");
Run Code Online (Sandbox Code Playgroud)

甚至更好:

PFNGLDRAWARRAYSINSTANCEDPROC ptr_glDrawArraysIndirect = ptr_to_glDrawArraysIndirect::ptr;
Run Code Online (Sandbox Code Playgroud)

ptr_to_glDrawArraysIndirect::ptr解开,如果它的定义或存根函数指针glDrawArraysIndirect stub_glDrawArraysIndirect否则.

我的目标操作系统非常具体.任何基于链接器的解决方案(如GetProcAddressdlsym)对我都不起作用,因为没有动态链接器.不止,我的驱动程序没有提供glXGetProcAdrress也没有wglGetProcAddress,基本上没有办法在运行时通过函数名查询指针(实际上,我想实现这样的机制).

有任何想法吗?

c++ opengl templates

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

std::function 的“潜在内存泄漏”

考虑这个例子:

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

using closure_type = std::function<void(void)>;
using closure_vec = std::vector<closure_type>;

class callbacks {
    static closure_type common(std::string name, uint32_t number) {
        return [number, name]() { std::cout << name << number << std::endl; };
    }
public:
    static closure_type foo(uint32_t number) { return common("foo ", number); }
    static closure_type print(std::string msg) {
        return [msg]() { std::cout << "print " << msg << std::endl; };
    }
};

template <typename... calls_t> closure_vec wrap(uint32_t number, calls_t &&... calls) { …
Run Code Online (Sandbox Code Playgroud)

c++ clang-tidy

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

在编译时迭代函数参数类型

C++ 有没有办法在编译时迭代函数参数类型?我想做这样的事情:

struct null_type {};

float foo(int, bool, char);

get_param_type<foo, 0>::type float_var; // return type
get_param_type<foo, 1>::type int_var; // first arg type
get_param_type<foo, 2>::type bool_var; // second arg type
get_param_type<foo, 3>::type char_var; // third arg type
get_param_type<foo, 4>::type null_type_var;
Run Code Online (Sandbox Code Playgroud)

c++ templates

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