I have a small snippet of code that compiles fine in clang repo head (3.5) but not in gcc 4.9 repo head. Although this looks like a gcc bug, before spamming the bugzilla I'd wanted to ask you if
- this is valid c++1y code (in the current draft state) - just because clang compiles it that's no reason for it to be correct code, and
- if anyone can reproduce this bug.
The code snippet compiling and running using clang is here:
http://coliru.stacked-crooked.com/a/acc691b9a407d6f2
However using
g++-4.9 -o main main.cpp -std=c++1y
gives me the aforementioned internal compiler error: http://pastebin.com/3fqV7xzC
Without the long dump it reads:
g++-4.9 -o main main.cpp -std=c++1y main.cpp: In instantiation of ‘composer::operator()(Func&&, Funcs&& ...):: [with auto:2 = float; Func = main(int, const char*)::; Funcs = {main(int, const char*)::}]’: main.cpp:33:88: required from here
main.cpp:19:41: internal compiler error: in retrieve_specialization, at cp/pt.c:1042
return f(c(std::forward<Funcs>(fs)...)(v));
^
For completeness' sake here is the snippet (the complete 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";
}
Edit: Bonus! I don't like that code at all - if anyone's got a nicer and probably faster way to do this consider to enlighten me...
Edit 2 Does anyone know how to get coliru (or a similar service) to use g++ 4.9?