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

Joh*_*ohn 3 c++ c++17

在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的顺序。

Ben*_*igt 5

我认为这应该工作:

using args_tuple_type = decltype( std::make_tuple(my_func(get<Is>(t))...) );
std::apply(some_func, args_tuple_type{ my_func(get<Is>(t))... });
Run Code Online (Sandbox Code Playgroud)

因为这些my_func调用都在braced-initializer-list中,所以它们将按顺序进行求值。 std::applysome_func呼叫所需的,但订购保证并非来自std::apply

并不是因为元组不是聚合,它是通过构造函数调用构造的,并且通常不会按顺序对构造函数的参数进行求值。但是,使用花括号仍会授予顺序,如