我有这样的代码,效果很好:
#include <algorithm>
#include <iostream>
char x[11]= "ABCDEFGHIJ";
char y[11];
struct F {
char operator () (char c) const
{ return c+1; }
};
int main()
{
std::transform(x, x+10, y, F());
y[10] = 0; std::cout <<y <<std::endl;
}
但如果我把它改成这种风格:
#include <algorithm>
#include <iostream>
char x[11]= "ABCDEFGHIJ";
char y[11];
int main()
{
struct F {
char operator () (char c) const
{ return c+1; }
};
std::transform(x, x+10, y, F());
y[10] = 0; std::cout <<y <<std::endl;
}
它不会编译,说:
错误:没有匹配的函数可用于调用“transform(char [11], char*, char [11], main()::F)”
怎么了?
gcc 版本是 4.4,它不识别 lambda 表达式。
在 C++-98/03 中,第二个代码无效,因为 F 是局部类型;事实上,第 14.3.1.2 节指出
本地类型、没有链接的类型、未命名类型或由任何这些类型复合的类型不得用作模板类型参数的模板参数。
[例子:
template <class T> class X { /* ... */ };
void f()
{
struct S { /* ... */ };
X<S> x3; // error: local type used as template-argument
X<S*> x4; // error: pointer to local type used as template-argument
}
—结束示例] [注意:模板类型参数可能是不完整类型(3.9)。 ]
在 C++-0x 中,此限制被删除;在同一部分中,新标准草案(N3126)在示例中明确表明了这一点:
[ 例子:
template <class T> class X { };
template <class T> void f(T t) { }
struct { } unnamed_obj;
void f() {
struct A { };
enum { e1 };
typedef struct { } B;
B b;
X<A> x1; // OK
X<A*> x2; // OK
X<B> x3; // OK
f(e1); // OK
f(unnamed_obj); // OK
f(b); // OK
}
—结束示例] [注意:模板类型参数可能是不完整类型(3.9)。 ——尾注]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)