ISO C++03 对函数作用域定义的结构体有哪些限制?

发布于 2024-11-16 05:40:14 字数 237 浏览 6 评论 0原文

我们不允许在函数内定义函子结构,因为不允许在函数模板的实例化中使用函数声明的结构。

还有其他需要注意的重大陷阱吗?例如,这会很糟糕吗:

int foo()
{
    struct Scratch
    {
        int a, b, c;
    };
    std::vector<Scratch> workingBuffer;
    //Blah Blah
}

We're not allowed to define a functor struct inside a function because one is not allowed to use function declared structs in the instantiation of function templates.

Are there any other significant pitfalls to be aware of? E.g. would this be bad:

int foo()
{
    struct Scratch
    {
        int a, b, c;
    };
    std::vector<Scratch> workingBuffer;
    //Blah Blah
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

回首观望 2024-11-23 05:40:14

1。 C++ 标准禁止使用带有模板的本地定义类。

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
    }

以下是来自 IBM 文档的更多参考:

2.本地类中的声明只能使用封闭范围中的类型名称、枚举、静态变量以及外部变量和函数。

代码示例:

int x;                         // global variable
void f()                       // function definition
{
      static int y;            // static variable y can be used by
                               // local class
      int x;                   // auto variable x cannot be used by
                               // local class
      extern int g();          // extern function g can be used by
                               // local class

      class local              // local class
      {
            int g() { return x; }      // error, local variable x
                                       // cannot be used by g
            int h() { return y; }      // valid,static variable y
            int k() { return ::x; }    // valid, global x
            int l() { return g(); }    // valid, extern function g
      };
}

int main()
{
      local* z;                // error: the class local is not visible
      return 0;
}

3.本地类不能有静态数据成员

代码示例:

void f()
{
    class local
    {
       int f();              // error, local class has noninline
                             // member function
       int g() {return 0;}   // valid, inline member function
       static int a;         // error, static is not allowed for
                             // local class
       int b;                // valid, nonstatic variable
    };
}

1. C++ standard forbids using locally-defined classes with templates.

14.3.1/2: 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.

A code 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
    }

Here is a little more reference from IBM documentation:

2. Declarations in a local class can only use type names, enumerations, static variables from the enclosing scope, as well as external variables and functions.

A Code Example:

int x;                         // global variable
void f()                       // function definition
{
      static int y;            // static variable y can be used by
                               // local class
      int x;                   // auto variable x cannot be used by
                               // local class
      extern int g();          // extern function g can be used by
                               // local class

      class local              // local class
      {
            int g() { return x; }      // error, local variable x
                                       // cannot be used by g
            int h() { return y; }      // valid,static variable y
            int k() { return ::x; }    // valid, global x
            int l() { return g(); }    // valid, extern function g
      };
}

int main()
{
      local* z;                // error: the class local is not visible
      return 0;
}

3. A local class cannot have static data members

A Code Example:

void f()
{
    class local
    {
       int f();              // error, local class has noninline
                             // member function
       int g() {return 0;}   // valid, inline member function
       static int a;         // error, static is not allowed for
                             // local class
       int b;                // valid, nonstatic variable
    };
}
宣告ˉ结束 2024-11-23 05:40:14

本地类的范围是定义它们的函数。但这本身并不有趣1

本地类的有趣之处在于如果它们实现了某个接口,那么您可以创建它的实例(使用new)并返回它们,从而使实现可以通过基类指针甚至在函数外部访问。

有关本地类的其他一些事实:

  • 它们不能定义静态成员变量。

  • 它们无法访问封闭函数的非静态“自动”局部变量。但它们可以访问静态变量。

  • 它们可以在模板函数中使用。但是,它们不能用作模板参数。

  • 如果他们在模板函数内部定义,那么他们可以使用外围函数的模板参数。

  • 本地类是最终的,这意味着函数外部的用户不能从本地类派生到函数。如果没有本地类,您必须在单独的翻译单元中添加未命名的命名空间。

  • 本地类用于创建trampoline 函数,通常称为 thunk

标准 (2003)

9.8 局部类声明 [class.local]

的一些参考资料

\1.类可以在函数定义中定义;这样一个类是
称为本地类。一个的名字
本地类是其封闭类的本地类
范围。本地类在范围内
的封闭范围,并且具有
对外部名称的相同访问权限
功能与封闭的一样
功能。本地声明
类只能使用类型名称,静态
变量、外部变量和
函数和枚举器
封闭范围。

[Example:

int x;
void f()
{
   static int s ;
   int x;
   extern int g();

   struct local {
      int g() { return x; } // error: x is auto
      int h() { return s; } // OK
      int k() { return ::x; } // OK
      int l() { return g(); } // OK
   };
// ...
}
local* p = 0; // error: local not in scope

—end example]

\2.封闭函数没有对本地成员的特殊访问权限
班级;它遵守通常的访问规则
(第 11 条)。 a 的成员函数
本地类应定义在
他们的类定义,如果他们是
完全定义。

\3.如果类 X 是本地类,则可以在其中声明嵌套类 Y
类 X 以及后来定义的
X类的定义或稍后
定义在相同的范围内
类 X 的定义。嵌套类
本地类中是本地类。

\4。本地类不应有静态数据成员。

The scope of the local classes is the function in which they're defined.But that isn't interesting in itself1.

What makes local classes interesting is that if they implement some interface, then you can create instances of it (using new) and return them, thereby making the implementation accessible through the base class pointer even outside the function.

Some other facts about local classes:

  • They cannot define static member variables.

  • They cannot access nonstatic "automatic" local variables of the enclosing function. But they can access the static variables.

  • They can be used in template functions. They cannot be used as template argument, however.

  • If they defined inside template function, then they can use the template parameters of the enclosing function.

  • Local classes are final, that means users outside the function cannot derive from local class to function. Without local classes, you'd have to add an unnamed namespace in separate translation unit.

  • Local classes are used to create trampoline functions usually known as thunks.

Some references from the Standard (2003)

9.8 Local class declarations [class.local]

\1. A class can be defined within a function definition; such a class is
called a local class. The name of a
local class is local to its enclosing
scope. The local class is in the scope
of the enclosing scope, and has the
same access to names outside the
function as does the enclosing
function. Declarations in a local
class can use only type names, static
variables, extern variables and
functions, and enumerators from the
enclosing scope.

[Example:

int x;
void f()
{
   static int s ;
   int x;
   extern int g();

   struct local {
      int g() { return x; } // error: x is auto
      int h() { return s; } // OK
      int k() { return ::x; } // OK
      int l() { return g(); } // OK
   };
// ...
}
local* p = 0; // error: local not in scope

—end example]

\2. An enclosing function has no special access to members of the local
class; it obeys the usual access rules
(clause 11). Member functions of a
local class shall be defined within
their class definition, if they are
defined at all.

\3. If class X is a local class a nested class Y may be declared in
class X and later defined in the
definition of class X or be later
defined in the same scope as the
definition of class X. A class nested
within a local class is a local class.

\4. A local class shall not have static data members.

过度放纵 2024-11-23 05:40:14

本地结构/类不能有静态数据成员,只能有静态成员函数。此外,它们不能成为模板。

Local structs / classes can't have static data members, only static member functions. Also, they can't be templates.

不甘平庸 2024-11-23 05:40:14

是的。 C++03 中局部类不能用作模板参数

Yes. Local classes can't be used as template parameters in C++03

匿名。 2024-11-23 05:40:14

本地结构是完全合法的,即使在 C++98 中也是如此。不过,您不能在 C++98 中将它们与模板一起使用,而在 C++0x 中则可以。 g++ 4.5 支持在 -std=c++0x 模式下使用带有模板的本地结构。

local structs are perfectly legal, even in C++98. You cannot use them with templates in C++98 though, whereas you can in C++0x. g++ 4.5 supports using local structs with templates in -std=c++0x mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文