3

Header File

#include <functional>

struct Check {};
struct Entry {};
struct Table {};

class SeatEntries
{
public:
    // does not compile!
    template<class T>
    void Populate(T from,
                  std::function<bool (Entry *)> predicate = [] (Entry *) { return true; },
                  std::function<void (Entry *)> action = [] (Entry *) {})
    {
        Reset();
        PopulateImpl(from, predicate, action);
    }

    // compiles!
    void PopulateFromCheck(Check *from,
                  std::function<bool (Entry *)> predicate = [] (Entry *) { return true; },
                  std::function<void (Entry *)> action = [] (Entry *) {})
    {
        Reset();
        //PopulateImpl(from, predicate, action);
    }

    void Reset();

private:
    template<class T>
    void PopulateImpl(T from,
                      std::function<bool (Entry *)> predicate,
                      std::function<void (Entry *)> action);
};

template<>
void SeatEntries::PopulateImpl<Table *>(Table *from, std::function<bool (Entry *)> predicate, std::function<void (Entry *)> action);

template<>
void SeatEntries::PopulateImpl<Check *>(Check *from, std::function<bool (Entry *)> predicate, std::function<void (Entry *)> action);

Question

Why doesn't the first member function template compile? Is this a bug in both VS2012 and gcc 4.8.1? Am I ignorant to some fact concerning member function templates and lambdas as default parameters?

VS2012 Output

error C2958: the left parenthesis '(' found at 'seathelpers.h(33)' was not matched correctly  
error C2988: unrecognizable template declaration/definition  
error C2059: syntax error : '{'  
error C2334: unexpected token(s) preceding '{'; skipping apparent function body  

The left parentheses referenced is the opening parentheses to the Populate() function template.

GCC 4.8.1 (MinGW)

Test.h:13:66: internal compiler error: in push_class_level_binding_1, at cp/name-lookup.c:3019  
    std::function<bool (Entry *)> predicate = [] (Entry *) { return true; },  
                                               ^
Praetorian
  • 106,671
  • 19
  • 240
  • 328
Osuvaldo
  • 274
  • 3
  • 10
  • 6
    An ICE *is* the result of a compiler bug. And yes, there are known bugs in g++4.8.1 with lambdas as default arguments. clang++ doesn't complain about this code. – dyp Aug 25 '14 at 16:53
  • Check the [bug tracker](https://gcc.gnu.org/bugzilla/) or update your compiler. –  Aug 25 '14 at 16:56
  • See, for example, http://stackoverflow.com/q/6025118 or http://stackoverflow.com/q/15589731 – dyp Aug 25 '14 at 16:56
  • @dyp Thanks, I should have read the entire output from gcc. I hastily tested it in gcc because VS2012 would not compile it. There is indeed already a bug report filed for this situation. – Osuvaldo Aug 25 '14 at 17:25

1 Answers1

1

I was able to compile the above code, without errors, with gcc 4.8.3

$ g++ --version
g++ (GCC) 4.8.3 20140624 (Red Hat 4.8.3-1)
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148