在测试某些功能的同时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) 为什么g()首先调用函数?我定义g()为初始化列表中的第二个元素。
以下与初始值设定项列表相关的标准引用是否相关?
\n\n\n\n\n\xc2\xa78.5.4.4:在花括号初始化列表的初始值设定项列表中,\n 初始值设定项子句,包括任何由包扩展产生的\n (\xc2\xa714.5.3),在它们出现的顺序。
\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}\nRun Code Online (Sandbox Code Playgroud)\n\n输出:
\n\ngf\nRun Code Online (Sandbox Code Playgroud)\n 在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的顺序。