小编Gio*_*ani的帖子

什么是<iosfwd>标题?

<iosfwd>标题用于什么(在此文件中提到)?

为什么有必要?

任何例子?

c++ iostream forward-declaration iosfwd

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

Why is my double buffer implementation 8x slower on Linux than Windows?

I've written this implementation of a double buffer:

// ping_pong_buffer.hpp

#include <vector>
#include <mutex>
#include <condition_variable>

template <typename T>
class ping_pong_buffer {
public:

    using single_buffer_type = std::vector<T>;
    using pointer = typename single_buffer_type::pointer;
    using const_pointer = typename single_buffer_type::const_pointer;

    ping_pong_buffer(std::size_t size)
        : _read_buffer{ size }
        , _read_valid{ false }
        , _write_buffer{ size }
        , _write_valid{ false } {}

    const_pointer get_buffer_read() {
        {
            std::unique_lock<std::mutex> lk(_mtx);
            _cv.wait(lk, [this] { return _read_valid; });
        }
        return _read_buffer.data();
    }

    void end_reading() {
        {
            std::lock_guard<std::mutex> lk(_mtx);
            _read_valid = …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading x86-64

9
推荐指数
2
解决办法
442
查看次数

va_list 参数实际上不是 va_list

尝试编译此代码时

#include <stdarg.h>

void bar_ptr(int n, va_list *pvl) {
    // do va_arg stuff here
}

void bar(int n, va_list vl) {
    va_list *pvl = &vl; // error here
    bar_ptr(n, pvl);
}

void foo(int n, ...) {
    va_list vl;
    va_list *pvl = &vl; // fine here
    va_start(vl, n);
    bar(n, vl);
    va_end(vl);
}

int main() {
    foo(3, 1, 2, 3);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

GCC 编译器会initialization from incompatible pointer typebar函数中打印一条警告。相同的语句在foo.

似乎 type 的 agument 的类型va_list不是 …

c variadic-functions

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

如果定义了枚举常量,如何静态断言?

我有这个 C++14 代码:

#include <type_traits>

struct f1 {
    enum struct e { a };
};

struct f2 {
    enum struct e {};
};

template <typename T>
struct my_struct {
    using e = typename T::e;
    my_struct()
    : _e{e::a} {} // e::a used here
    e _e;
};

int main() {
    my_struct<f1> a;
    my_struct<f2> b; // compilation fails
}
Run Code Online (Sandbox Code Playgroud)

显然,编译失败,类似于'a' is not a member of 'my_struct<f2>::e'. 我真的很想向 中添加一些静态断言my_struct,以添加自定义错误消息。首先,我可以检查是否e实际上是一个枚举:

static_assert(std::is_enum<e>::value, "my message");
Run Code Online (Sandbox Code Playgroud)

那么,我应该添加什么来静态断言e::a定义的?

c++ static-assert c++14

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

将可变参数模板与 C 风格可变参数函数混合时的模板参数推导

这个答案的启发,我生成了这段代码,其输出取决于编译器:

template <typename... Args>
constexpr auto foo(Args&& ...args, ...) noexcept {
    return sizeof...(args);
}

constexpr auto bar() noexcept {
    return (&foo<int>)(1, 2);
}
Run Code Online (Sandbox Code Playgroud)

如果使用 GCC 11 编译,bar则调用foo<int>并返回 1,而 clang 13 和 MSVC 2019 都会推导foo<int, int>bar返回 2。

这是我在 godbolt 上的沙箱:https ://godbolt.org/z/MedvvbzqG 。

哪个输出是正确的?

编辑:

如果我return foo<int>(1, 2);直接使用,即与

constexpr auto bar() noexcept {
    return foo<int>(1, 2);
}
Run Code Online (Sandbox Code Playgroud)

沙箱更新:https://godbolt.org/z/Wj757sc7b

c++ variadic-functions language-lawyer variadic-templates template-argument-deduction

7
推荐指数
1
解决办法
306
查看次数

std::string_view 的 noexcept 构造函数

根据文档, std::string_view 有一个带有 aconst char *和 a的构造函数std::size_t,但未声明noexcept

constexpr basic_string_view(const CharT* s, size_type count);
Run Code Online (Sandbox Code Playgroud)

另一方面,文档还声明了用户定义的文字operator""sv,在我见过的所有实现中都是该构造函数的简单包装器,已声明noexcept

constexpr std::string_view operator "" sv(const char* str, std::size_t len) noexcept;
Run Code Online (Sandbox Code Playgroud)

你知道这种差异的原因吗?构造函数什么时候可以抛出?

c++ user-defined-literals noexcept string-view c++17

6
推荐指数
1
解决办法
742
查看次数

使用结构体本身作为模板参数是否合法?

根据cppreference.com 上的模板参数和模板参数:

类型模板参数的模板参数必须是类型 ID,它可能会命名不完整的类型

这意味着这个示例(从该页面复制)是合法的:

// Example 0

template <typename T>
struct X {};

struct A;

int main() {
    X<A> x1;
}
Run Code Online (Sandbox Code Playgroud)

而且,我想,这也是合法的:

// Example 1

template <typename T>
struct X {};

struct A {
    X<A> x1;
};

int main() {
    A a1;
}
Run Code Online (Sandbox Code Playgroud)

现在,让我们使代码复杂一些:

// Example 2

template <typename T>
struct X {
    using type = typename T::type;
};

struct A {
    using type = int;
    X<A>::type x1;
};

int main() {
    A a1;
}
Run Code Online (Sandbox Code Playgroud)

在这种情况下,A …

c++ templates language-lawyer incomplete-type c++14

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

编译器是否允许省略对指针的 &amp;* 运算符的组合调用?

我有这个模板功能

template <typename T, typename It, std::enable_if_t<std::is_integral<T>::value, int> = 0>
inline T decode(It &it) {
    static_assert(std::is_same<typename std::iterator_traits<It>::value_type, std::uint8_t>::value, "invalid");
    T* v_p = reinterpret_cast<T*>(&*it);
    it += sizeof(T);
    return *v_p;
}
Run Code Online (Sandbox Code Playgroud)

用于从原始指针解码整数。该函数可以与任何具有迭代器特征的类型一起使用,即指向具有满足LegacyContiguousIterator要求的迭代std::uint8_t器的std::uint8_tSTL 容器的指针或迭代器。

该函数有效,但我不确定调用&*itwhenit是指针的性能。需要运算符来从迭代器获取指针,如本答案中所述,但对于 POD 指针来说似乎有点过分。是否允许编译器只删除操作,或者最好为指针编写专门化

c++ pointers c++14

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

LLVM 7.0在Visual Studio 2015上未提供特定的平台工具集

我刚刚安装了几个小时前发布的Windows(64位)LLVM 7.0.0。

对于新版本,Visual Studio 2015的先前版本(6.0.1)提供的所有平台工具集(名为“ LLVM-vs2014 ”,“ LLVM-vs2014_xp ”和“ LLVM-vs2010 ”)不再可用。

我在Visual Studio Marketplace中看到了一个名为LLVM Compiler Toolchain的新扩展,但不适用于Visual Studio 2015。

你知道这种缺乏的原因吗?

llvm clang visual-studio visual-c++ visual-studio-2015

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

内存泄漏Valgrind

我写了一个程序,用五个字符串串起一个字符串.这是我的计划.

struct list
{
    char *str;
    struct list* next;
};

struct list* head = NULL;

void insert(char *cont)
{
    struct list* temp = (struct list*)malloc(sizeof(struct list));

    size_t len = strlen(cont);
    char *heapString = (char*)malloc(len);
    strcpy(heapString,cont);

    temp->str = heapString;
    temp->next = NULL;

    if(head == NULL)
    {
        head = temp;
        return ;
    }

    temp->next = head;
    head = temp;
}
void print()
{
    struct list* temp = head;
    while(temp != NULL)
    {
        printf("%s\n",temp->str);
        temp = temp->next;
    }
}
void clearmem()
{
    struct …
Run Code Online (Sandbox Code Playgroud)

c linux valgrind

0
推荐指数
1
解决办法
171
查看次数