为参数包的每个参数扩展一个 lambda:Clang 与 GCC
Expanding a lambda for each parameter of a parameter pack: Clang vs. GCC
此代码在 Clang 3.5 中运行良好:
#include <iostream>
#include <string>
void callFuncs() {}
template<typename Func, typename ...Funcs>
void callFuncs(const Func &func, const Funcs &...funcs)
{
func();
callFuncs(funcs...);
}
template<typename ...Types>
void callPrintFuncs()
{
callFuncs(([] { std::cout << Types() << std::endl; })...);
}
int main()
{
callPrintFuncs<int, float, double, std::string>();
}
但是,在 GCC 4.9 中,我收到以下错误:
test.cpp: In lambda function:
test.cpp:16:54: error: parameter packs not expanded with '...':
callFuncs(([] { std::cout << Types() << std::endl; })...);
^
test.cpp:16:54: note: 'Types'
test.cpp: In function 'void callPrintFuncs()':
test.cpp:16:58: error: expansion pattern '<lambda>' contains no argument packs
callFuncs(([] { std::cout << Types() << std::endl; })...);
那么,哪个编译器有 bug,Clang 还是 GCC? Clang 行为至少对我来说最有意义。
gcc 在这里坏掉了。标准中有禁止未扩展参数包的规则,但是上面的参数包是扩展的
在最里面的语句结束后展开,但标准不要求在每个语句结束时展开参数包。
gcc 弄错了这一事实在某种程度上是可以理解的;天真地,你会认为一个参数包只能在一个语句中,而在语句末尾扩展失败是致命的。但是 lambda 允许您在语句中嵌套语句。
一般的解决方法是传入一个 lambda 并向其传入 "tag" 类型。
template<class T>struct tag_t{using type=T;};
template<class Tag>using type_t=typename Tag::type;
template<typename Func, typename ...Ts>
void callOnEachOf(Func&&func, Ts&&...ts)
{
using discard=int[];
(void)discard{0,((void)(
func(std::forward<Ts>(ts))
),0)...};
}
template<typename ...Types>
void callPrintFuncs()
{
callOnEachOf(
[](auto tag){
using Type=type_t<decltype(tag)>;
std::cout << Type() << std::endl;
},
tag_t<Types>...
);
}
此代码在 Clang 3.5 中运行良好:
#include <iostream>
#include <string>
void callFuncs() {}
template<typename Func, typename ...Funcs>
void callFuncs(const Func &func, const Funcs &...funcs)
{
func();
callFuncs(funcs...);
}
template<typename ...Types>
void callPrintFuncs()
{
callFuncs(([] { std::cout << Types() << std::endl; })...);
}
int main()
{
callPrintFuncs<int, float, double, std::string>();
}
但是,在 GCC 4.9 中,我收到以下错误:
test.cpp: In lambda function:
test.cpp:16:54: error: parameter packs not expanded with '...':
callFuncs(([] { std::cout << Types() << std::endl; })...);
^
test.cpp:16:54: note: 'Types'
test.cpp: In function 'void callPrintFuncs()':
test.cpp:16:58: error: expansion pattern '<lambda>' contains no argument packs
callFuncs(([] { std::cout << Types() << std::endl; })...);
那么,哪个编译器有 bug,Clang 还是 GCC? Clang 行为至少对我来说最有意义。
gcc 在这里坏掉了。标准中有禁止未扩展参数包的规则,但是上面的参数包是扩展的
在最里面的语句结束后展开,但标准不要求在每个语句结束时展开参数包。
gcc 弄错了这一事实在某种程度上是可以理解的;天真地,你会认为一个参数包只能在一个语句中,而在语句末尾扩展失败是致命的。但是 lambda 允许您在语句中嵌套语句。
一般的解决方法是传入一个 lambda 并向其传入 "tag" 类型。
template<class T>struct tag_t{using type=T;};
template<class Tag>using type_t=typename Tag::type;
template<typename Func, typename ...Ts>
void callOnEachOf(Func&&func, Ts&&...ts)
{
using discard=int[];
(void)discard{0,((void)(
func(std::forward<Ts>(ts))
),0)...};
}
template<typename ...Types>
void callPrintFuncs()
{
callOnEachOf(
[](auto tag){
using Type=type_t<decltype(tag)>;
std::cout << Type() << std::endl;
},
tag_t<Types>...
);
}