【发布时间】:2013-04-07 17:17:00
【问题描述】:
在初始化列表中包含可变参数模板的参数应该确保它们按顺序进行评估,但这里不会发生:
#include <iostream>
using namespace std;
template<class T> void some_function(T var)
{
cout << var << endl;
}
struct expand_aux {
template<typename... Args> expand_aux(Args&&...) { }
};
template<typename... Args> inline void expand(Args&&... args)
{
bool b[] = {(some_function(std::forward<Args>(args)),true)...}; // This output is 42, "true", false and is correct
cout << "other output" << endl;
expand_aux temp3 { (some_function(std::forward<Args>(args)),true)... }; // This output isn't correct, it is false, "true", 42
}
int main()
{
expand(42, "true", false);
return 0;
}
怎么会?
【问题讨论】:
-
编译器错误?从您提供的链接中,从 gcc 切换到 clang 或 Intel 将产生您期望的结果。
-
你说得对,Drew,clang 做得对
-
(或未定义的行为...)
-
我可能错了,但这可能是评估问题的顺序吗?是否有任何东西迫使调用,所以 some_function 以您期望的顺序进行评估?编辑:从 Andy Prowl 那里得到了我的回答。谢谢!
-
VC++ 2013 也有同样的问题。 :-[ 此处报告:connect.microsoft.com/VisualStudio/feedback/details/1075443
标签: c++ templates c++11 variadic-templates