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) 尝试编译此代码时
#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 type在bar函数中打印一条警告。相同的语句在foo.
似乎 type 的 agument 的类型va_list不是 …
我有这个 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定义的?
受这个答案的启发,我生成了这段代码,其输出取决于编译器:
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)
c++ variadic-functions language-lawyer variadic-templates template-argument-deduction
根据文档, 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)
你知道这种差异的原因吗?构造函数什么时候可以抛出?
根据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 …
我有这个模板功能
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 指针来说似乎有点过分。是否允许编译器只删除操作,或者最好为指针编写专门化
我刚刚安装了几个小时前发布的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。
你知道这种缺乏的原因吗?
我写了一个程序,用五个字符串串起一个字符串.这是我的计划.
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)