头文件
#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);
题
为什么第一个成员函数模板不编译?这是VS2012和gcc 4.8.1中的错误吗?我是否不了解有关成员函数模板和lambda作为默认参数的某些事实?
VS2012输出
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
引用的左括号是Populate()函数模板的左括号。
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; },
^
最佳答案
我能够使用gcc 4.8.3正确编译以上代码
$ g++ --version
g++ (GCC) 4.8.3 20140624 (Red Hat 4.8.3-1)