有人能告诉我这个代码出了什么问题吗:
template<typename B=int> // <<<< =int is provoking the error
struct Foo
{
std::array<B,10> data;
template<typename F>
void iterate(F f) const
{
for(unsigned i=0; i<10; i++) {
f(i, data[i]);
}
}
friend std::ostream& operator<<(std::ostream& os, const Foo<B>& a) // Line 17
{
a.iterate([&os](unsigned i, const B& x) {
os << i << "=" << x << "n";
});
return os;
}
};
带有GCC 4.8.1和--std=c++11
:的错误消息
test.cpp: In function ‘std::ostream& operator<<(std::ostream&, const Foo<B>&)’:
test.cpp:17:41: error: default argument for template parameter for class enclosing ‘operator<<(std::ostream&, const Foo<B>&)::__lambda0’
a.iterate([&os](unsigned i, const B& x) {
a.iterate([&os](unsigned i, const B& x) {
os << i << "=" << x << "n";
});
这里引入的lambda函数的作用是在operator<<
函数体的函数范围内声明一个"未命名"的局部类。我们可以编写自己的函子对象来获得类似的效果:
struct lambda {
std::ostream& os;
lambda(std::ostream& os_) : os(os_) {}
void operator()(unsigned i, const B& x) {
os << i << "=" << x << "n";
}
};
a.iterate(lambda(os));
这从g++中引出了类似的错误:
error: default argument for template parameter for class enclosing
‘operator<<(std::ostream&, const Foo<B>&)::lambda::lambda’
这里有一个SSCCE:
template <typename T = int>
struct Foo
{
friend void f() {
struct local {};
}
};
它引发的错误:
error: default argument for template parameter for class enclosing
‘f()::local::local’
这已经被报告为GCC错误57775。
正如@PiotrNycz所指出的,您可以通过将函数operator<<
的主体移动到类模板之外来解决问题。