我有一小段代码在clang repo head(3.5)中编译得很好,但在gcc 4.9 repo head中却没有。虽然这看起来像一个 gcc 错误,但在向 bugzilla 发送垃圾邮件之前,我想问你是否
- 这是有效的 c++1y 代码(处于当前草稿状态) - 仅仅因为 clang 编译它,它没有理由成为正确的代码,并且
- 如果有人可以重现此错误。
使用 clang 编译运行的代码片段如下:
http://coliru.stacked-crooked.com/a/acc691b9a407d6f2
但是使用
g++-4.9 -o main main.cpp -std=c++1y
给了我前面提到的内部编译器错误: http: //pastebin.com/3fqV7xzC
如果没有长转储,它会显示:
g++-4.9 -o main main.cpp -std=c++1y main.cpp: 在 'composer::operator()(Func&&, Funcs&& ...):: [with auto:2 = float; Func = main(int, const char* )::; Funcs = {main(int, const char *)::}]': main.cpp:33:88: 这里需要
main.cpp:19:41: internal compiler error: in retrieve_specialization, at cp/pt.c:1042
return f(c(std::forward<Funcs>(fs)...)(v));
^
为了完整起见,这里是片段(完整的 main.cpp)
#include <iostream>
#include <utility>
template <typename... Funcs>
struct composer;
template <>
struct composer<> {
auto operator()() {
return [&] (auto v) { return v; };
}
};
template <typename Func, typename... Funcs>
struct composer<Func, Funcs...> {
auto operator()(Func&& f, Funcs&&... fs) {
composer<Funcs...> c;
return [&] (auto v) {
return f(c(std::forward<Funcs>(fs)...)(v));
};
}
};
template <typename... Funcs>
auto compose(Funcs&&... fs) {
composer<Funcs...> c;
return c(std::forward<Funcs>(fs)...);
}
int main (int argc, char const* argv[]) {
float v = 3.5f;
auto t = compose([] (auto v) { return v >= 3; }, [] (auto v) { return int(v-0.5); })(v);
std::cout << std::boolalpha << t << "\n";
auto f = compose([] (auto v) { return v > 3; }, [] (auto v) { return int(v-0.5); })(v);
std::cout << std::boolalpha << f << "\n";
}
编辑:奖金!我根本不喜欢该代码-如果有人有更好且可能更快的方法来执行此操作,请考虑启发我...
编辑 2有谁知道如何让 coliru(或类似服务)使用 g++ 4.9?