问题描述
在,@Dietmar给出了此解决方案:
In the other topic, @Dietmar gave this solution:
template <typename... T>
std::tuple<T...> parse(std::istream& in)
{
return std::tuple<T...>{ T(in)... };
}
说明
使用括号初始化工作原理是因为括号初始值列表中的参数求值顺序是它们出现的顺序。 (强调我)
C ++标准(n3485)的相关文本是
The relevant text from the C++ Standard (n3485) is,
所以我试着用下面的代码测试这个: / p>
So I tried to test this with the following code:
template<int N>
struct A
{
std::string data;
A(std::istream & stream) { stream >> data; }
friend std::ostream& operator<<(std::ostream & out, A<N> const & a)
{
return out << "A"<<N<<"::data = " << a.data;
}
};
typedef A<1> A1;
typedef A<2> A2;
template<typename ...Args>
void test(std::istream & stream)
{
std::tuple<Args...> args { Args(stream)... };
std::cout << std::get<0>(args) << std::endl;
std::cout << std::get<1>(args) << std::endl;
}
int main()
{
std::stringstream ss("A1 A2");
test<A1,A2>(ss);
}
预期输出:
A1::data = A1
A2::data = A2
b $ b
实际输出:
Actual Output:
A1::data = A2
A2::data = A1
我在测试代码中做错了什么?我将我的代码更改为:
Did I do anything wrong in my test code? I changed my code to this:
std::stringstream ss("A1 A2");
std::tuple<A1,A2> args{A1(ss), A2(ss)};
std::cout << std::get<0>(args) << std::endl;
std::cout << std::get<1>(args) << std::endl
与以前相同的输出。我用 MinGW(GCC)4.7.0
和 4.7.2
测试我的代码。即使是ideone也会。
Same output as before. I tested my code with MinGW (GCC) 4.7.0
and 4.7.2
. Even ideone gives this output.
这是编译器中的错误吗? / p>
Is it a bug in the compiler?
推荐答案
是的。这是GCC编译器中的一个错误。
Yes. It is a bug in the GCC compiler.
- 中的initializer-clause中的评估顺序>
- Bug 51253 - [C++11][DR 1030] Evaluation order (sequenced-before relation) among initializer-clauses in braced-init-list
取自@Johannes Schaub对问题的意见。
taken from @Johannes Schaub's comment to the question.
这篇关于列表初始化中元素的求值顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!