小编tsn*_*rri的帖子

Template parameter type deduction fails in GCC, not in Clang

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 …

c++ templates language-lawyer c++11

9
推荐指数
0
解决办法
244
查看次数

允许在C++中访问容器对象

我有一个类似下面的类:

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++

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

同时修改std :: valarray <T>的元素是否安全?

如果我已经正确理解,由于C++ 11它一直安全只要同时调用一个容器的const成员函数和修改容器中的元素作为容器本身没有被修改作为操作的一部分(如从可见例如cppreference.com中有关线程安全的表格.由于std :: valarray没有在(草案)标准的容器部分列出,我不确定线程​​安全是否也适用于它.换一种说法,

  • 同时从std :: valarray读取是否安全(特别是使用带有切片的operator [])?
  • 如果对T的操作是安全的,同时修改std :: valarray <T>的元素是否安全?

我想使用std :: valarray作为使用多个线程填充的多维数字数组.

c++ multithreading c++11 valarray

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

模板专业化的模板

有没有办法从模板专业化中获取模板?例如,std::unordered_mapstd::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)

c++ templates

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

boost::process::child 在关闭输入流后不会退出

在以下示例中,我尝试将一些数据写入子进程,该子进程处理数据并将其写入文件。关闭流后,父进程无限期地等待子进程完成。我不知道如何表明我已经完成了数据的写入,并希望子进程停止读取并完成它正在做的任何事情。根据文档调用 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)

c++ boost boost-process

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