Consider the following code (Godbolt):
#include <tuple>
#include <type_traits> // std::false_type, std::true_type
template <typename t_tuple, auto t_size = std::tuple_size_v<t_tuple>>
struct test : std::false_type {};
template <typename t_tuple>
struct test<t_tuple, 0> : std::true_type {};
int main(int argc, char **argv)
{
static_assert(test<std::tuple<>>::value);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Clang and some other compilers accept the code but in GCC and ICC the static assertion fails. Either changing the type of t_size from auto to std::size_t or casting the zero in the specialisation to …
我有一个类似下面的类:
class Foo {
private:
std::map<std::string, Bar*> bars_by_name;
std::map<std::string, Baz*> bazs_by_name;
};
Run Code Online (Sandbox Code Playgroud)
现在我想允许用户访问两个集合,但隐藏我将对象存储到std :: maps中的实现细节.相反,我希望有成员函数返回例如集合的const迭代器,甚至可能是从两个集合返回对象的自定义迭代器,因为Bar和Baz属于同一个类层次结构.考虑到样式,在C++中执行此操作的正确方法是什么?在Java中,我可能会设置方法的返回类型为可迭代或包裹收集到unmodifiableCollection.
如果我已经正确理解,由于C++ 11它一直安全只要同时调用一个容器的const成员函数和修改容器中的元素作为容器本身没有被修改作为操作的一部分(如从可见例如cppreference.com中有关线程安全的表格.由于std :: valarray没有在(草案)标准的容器部分列出,我不确定线程安全是否也适用于它.换一种说法,
我想使用std :: valarray作为使用多个线程填充的多维数字数组.
有没有办法从模板专业化中获取模板?例如,std::unordered_map从std::unordered_map<char, char>要传递的类型变量作为模板模板参数.
最小的例子:
#include <unordered_map>
template <template <class ...> class t_map>
class A
{
public:
typedef t_map <int, int> map_type;
};
int main(int argc, char const **argv)
{
std::unordered_map<char, char> map;
// decltype yields std::unordered_map<char, char> (as expected).
typename A<decltype(map)>::map_type map_2;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在以下示例中,我尝试将一些数据写入子进程,该子进程处理数据并将其写入文件。关闭流后,父进程无限期地等待子进程完成。我不知道如何表明我已经完成了数据的写入,并希望子进程停止读取并完成它正在做的任何事情。根据文档调用 terminate会发送一个SIGKILL我认为不是我想要的。
我错过了什么?我检查了这个问题,但我宁愿先尝试使实际代码与同步 IO 一起工作。
#include <boost/process.hpp>
#include <iostream>
namespace bp = boost::process;
int main(int argc, char **argv)
{
boost::process::opstream in{};
boost::process::child child("/path/to/test.py", bp::std_in < in);
in << "test1\n";
in << "test2\n";
in << "test3\n";
in << std::flush;
std::cerr << "Closing the stream…\n";
in.close();
std::cerr << "Waiting for the child to exit…\n";
child.wait(); // Parent seems to hang here.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
test.py 只是将数据写入文件,如下所示:
#!/usr/local/homebrew/opt/python@3.8/bin/python3
import sys
with open("/tmp/test.txt", "w") as f:
for line in …Run Code Online (Sandbox Code Playgroud)