相关疑难解决方法(0)

(优化?)关于GCC std :: thread的Bug

在测试某些功能的同时std::thread,一位朋友遇到了GCC的问题,我们认为值得问一下这是否是GCC错误或者这个代码可能有问题(代码打印(例如)"7 8 9 10 1 2 3" ,但我们希望打印[1,10]中的每个整数:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <thread>

int main() {
    int arr[10];
    std::iota(std::begin(arr), std::end(arr), 1);
    using itr_t = decltype(std::begin(arr));

    // the function that will display each element
    auto f = [] (itr_t first, itr_t last) {
        while (first != last) std::cout<<*(first++)<<' ';};

    // we have 3 threads so we need to figure out the ranges for each thread to show
    int increment = std::distance(std::begin(arr), std::end(arr)) / 3;
    auto first …
Run Code Online (Sandbox Code Playgroud)

multithreading clang c++11 gcc4.7

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

初始化列表中元素的求值顺序

为什么g()首先调用函数?我定义g()为初始化列表中的第二个元素。

\n\n

以下与初始值设定项列表相关的标准引用是否相关?

\n\n
\n

\xc2\xa78.5.4.4:在花括号初始化列表的初始值设定项列表中,\n 初始值设定项子句,包括任何由包扩展产生的\n (\xc2\xa714.5.3),在它们出现的顺序。

\n
\n\n
#include <iostream>\n#include <vector>\n\nint f() { std::cout << "f"; return 0;}\nint g() { std::cout << "g"; return 0;}\n\nvoid h(std::vector<int> v) {}\n\nint main() {\n\n   h({f(), g()});\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

输出:

\n\n
gf\n
Run Code Online (Sandbox Code Playgroud)\n

c++ gcc operator-precedence

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

std :: apply是否为评估顺序提供保证?

在c ++中,未指定作为函数调用参数提供的表达式的求值顺序。当我使用std :: apply时,是否可以保证在元组的元素上调用该函数?我有一种情况很重要,那就是该函数首先应用于元组的第一个元素,然后应用于第二个,然后应用于第三个元素...。

作为反例:

template <class Tuple, size_t... Is>
void function(Tuple t, std::index_sequence<Is...>) {
    some_func( my_func(get<Is>(t))... );
}
Run Code Online (Sandbox Code Playgroud)

不能保证在元组的每个元素上调用my_func的顺序。

c++ c++17

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

标签 统计

c++ ×2

c++11 ×1

c++17 ×1

clang ×1

gcc ×1

gcc4.7 ×1

multithreading ×1

operator-precedence ×1