为什么使用局部struct作为STL函数的参数的代码不能在g ++中编译? [英] Why code using local struct as parameter for STL function does not compile in g++?

查看:157
本文介绍了为什么使用局部struct作为STL函数的参数的代码不能在g ++中编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码工作正常:

  #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表达式。 p>

解决方案

在C ++ - 98/03中,第二个代码无效,因为F是一个局部类型;事实上,在§14.3.1.2它表示


本地类型,没有链接的类型,未命名类型或类型复合



[示例:




  template< class T> class X {/ * ... * /}; 
void f()
{
struct S {/ * ... * /};
X< S> x3; //错误:用作模板参数的局部类型
X< S *> x4; //错误:指向用作模板参数的本地类型的指针
}




-end example] [注意:模板类型参数可能是不完整的类型(3.9)。 ]


在C ++ - 0x中,在同一节中,新标准草案(N3126)在示例中明确显示了这一点:


[示例:




 模板< 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 *> ×2; // OK
X< B> x3; // OK
f(e1); // OK
f(unnamed_obj); // OK
f(b); // OK
}




:模板类型参数可能是不完整的类型(3.9)。 - end note]



I have such code which works well:

#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;
}

But if I change it to this style:

#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;
}

It will not compile, saying:

error: no matching function for call to ‘transform(char [11], char*, char [11], main()::F)’

What's wrong?

gcc version is 4.4, which does not recognize lambda expressions.

解决方案

In C++-98/03 the second code is not valid, since F is a local type; in facts, at §14.3.1.2 it's stated that

A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

[Example:

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
}

—end example] [Note: a template type argument may be an incomplete type (3.9). ]

In C++-0x this limitation is removed; in the same section, the new standard draft (N3126) explicitly shows this in the example:

[ Example:

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
}

— end example ] [ Note: a template type argument may be an incomplete type (3.9). — end note ]

这篇关于为什么使用局部struct作为STL函数的参数的代码不能在g ++中编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆