为什么使用本地struct作为STL函数参数的代码不能用g ++编译?

RnM*_*Mss 11 c++ templates stl g++ c++11

我有这样的代码,效果很好:

#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;
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我将其更改为此样式:

#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;
}
Run Code Online (Sandbox Code Playgroud)

它不会编译,说:

错误:没有匹配函数来调用'transform(char [11],char*,char [11],main():: F)'

怎么了?

gcc版本是4.4,它不识别lambda表达式.

Mat*_*lia 10

在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
}
Run Code Online (Sandbox Code Playgroud)

-end example] [注意:模板类型参数可能是不完整类型(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
}
Run Code Online (Sandbox Code Playgroud)

- 结束示例] [注意:模板类型参数可能是不完整类型(3.9). - 结束说明]

  • @Yttrill嗯,我也这么认为.但是抬头看来,似乎C++ 03实际上并不是TC,而是C++的全新版本.这似乎与GCC和Clang相矛盾,它只有-std = c ++ 98并且说"1998 ISO C++标准加上修正.与C++代码的-ansi相同.".奇怪的! (3认同)