#include <vector>
#include <string>
#include <mutex>
#include <future>
using namespace std;
mutex g_mtx;
vector<string> g_coll;
void Cleaner()
{
lock_guard<mutex> lock(g_mtx);
g_coll.clear();
}
const vector<string>& Getter()
{
lock_guard<mutex> lock(g_mtx);
return g_coll;
}
int main()
{
g_coll = { "hello" };
auto fut = async([&]()
{
Cleaner();
});
auto returned_coll = Getter();
fut.get();
}
Run Code Online (Sandbox Code Playgroud)
如果Cleaner是后执行return g_coll;,不C++标准保证的是returned_coll包含{ "hello" }?
请考虑http://en.cppreference.com/w/cpp/experimental/when_any.下面只是一个幼稚和简单的实现:
#include <future>
template<typename Iterator>
auto when_any(Iterator first, Iterator last)
{
while (true)
{
for (auto pos = first; pos != last; ++pos)
{
if (pos->is_ready())
{
return std::move(*pos);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不满意,因为它是一个无限循环中的繁忙轮询.
有没有办法避免繁忙的民意调查?
Linux系统头文件中定义了以下结构/usr/include/sys/inotify.h:
struct inotify_event
{
int wd;
uint32_t mask;
uint32_t cookie;
uint32_t len;
char name __flexarr;
};
Run Code Online (Sandbox Code Playgroud)
请注意最后一个字段name,它是一个零长度数组.C++ 17不支持零长度的阵列,所以,如果使用struct inotify_event以C++ 17项目和与编译它-pedantic,编译器警告应该提高.
但是,以下代码不会引发任何警告struct inotify_event.更奇怪的是,如果我以相同的方式使用零长度数组定义结构,则会按预期引发警告.
编译器选项:clang ++ -std = c ++ 17 -pedantic main.cpp
#include <sys/inotify.h>
struct inotify_event* p = nullptr; // no warning
struct A
{
int x;
char name __flexarr; // warning: flexible array members are a C99 feature
};
int main()
{}
Run Code Online (Sandbox Code Playgroud)
clang ++背后是否有任何魔术以更轻松的方式处理系统头?
#include <iostream>
#include <type_traits>
using namespace std;
template<typename T>
void f(T&&)
{
cout << boolalpha << std::is_const_v<T> << endl;
cout << boolalpha << std::is_const_v<T&&> << endl;
}
int main()
{
const int n = 1;
f(n);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
false
false
Run Code Online (Sandbox Code Playgroud)
这里n是一个明显的const变量,为什么不std::is_const_v按预期运行?
#include <set>
int main()
{
auto coll = std::multiset{ 1, 2, 2, 3, 4, 4, 7 };
}
Run Code Online (Sandbox Code Playgroud)
上面的代码可以用vc++ 2019和编译g++ 9.0,但不能用clang++ 8.0as 编译clang++ -std=c++2a -stdlib=libc++ main.cpp
为什么libc ++不支持多集上的类模板参数推导?
看了的手册msync,我认为 的确切含义MS_INVALIDATE如下:
假设有三个进程p1、p2和p3。
p1和p2都使用mmapwithMAP_SHARED来同时读写文件/tmp/data.txt。
p3用于fread读取同一文件。
假设p1修改了文件,p2将立即看到修改。然而,p3使用fread并不确定是否能看到修改。
如果p1在修改后调用msyncwith ,则p3 using肯定会看到修改。这就是旗帜的全部含义。MS_INVALIDATE|MS_SYNCfreadMS_INVALIDATE
我的理解正确吗?
struct A
{
void f1()
{
f2(); // ok, though f2() is not declared before
}
void f2()
{}
void f3(X*) // error: unknown type name 'X'
{}
struct X
{};
};
int main()
{
A a;
}
Run Code Online (Sandbox Code Playgroud)
为什么成员类型需要前向声明而成员函数不需要?
C++20 的新特性之一是Down with typename。
在 C++17 中,您必须在几乎所有†依赖上下文中提供 typename 关键字以消除类型与值的歧义。但是在 C++20 中,这个规则放宽了很多。在需要类型的所有上下文中, typename 关键字不再是必需的。
template<typename T>
concept IsOK = true;
template<typename T>
requires IsOK<T::U> // error: use ‘typename T::U’
void f()
{}
struct A
{
using U = int;
};
int main()
{
f<A>();
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,显然,IsOK概念只能接受类型。
为什么 typename 这里需要?
看在线演示
根据cppref:
constexpr iterator insert( const_iterator pos, const T& value );返回值
指向插入值的迭代器。
复杂
pos 与容器末端之间距离的常数加上线性。
例外情况
如果在末尾插入单个元素时抛出异常,并且 T 为 CopyInsertable 或 std::is_nothrot_move_constructible::value 为 true,则不会产生任何影响(强异常保证)。
如果pos无效,说明文档没有明确描述以下问题:
所以,我的问题是:
std::vector::insert(pos, value) 如果无效 怎么办pos?
c++ ×9
standards ×7
c ×3
linux ×3
c++11 ×2
c++17 ×2
c++20 ×2
clang ×2
c++-concepts ×1
class ×1
concurrency ×1
const ×1
exception ×1
filesystems ×1
glibc ×1
libc++ ×1
locking ×1
mmap ×1
overloading ×1
templates ×1
type-traits ×1
typename ×1
types ×1
vector ×1