Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What restrictions does ISO C++03 place on structs defined at function scope?

Tags:

c++

c++03

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
}
like image 353
Billy ONeal Avatar asked Jun 20 '11 18:06

Billy ONeal


People also ask

Is it illegal to define a function within a structure?

You can only define a function pointer in a struct in C programming language which is different from C++.

Are functions allowed in struct?

1. Member functions inside the structure: Structures in C cannot have member functions inside a structure but Structures in C++ can have member functions along with data members.

Where do you declare structs?

The general syntax for a struct declaration in C is: struct tag_name { type member1; type member2; /* declare as many members as desired, but the entire structure size must be known to the compiler. */ };

How do you define a struct in C++?

The struct keyword defines a structure type followed by an identifier (name of the structure). Then inside the curly braces, you can declare one or more members (declare variables inside curly braces) of that structure. For example: struct Person { char name[50]; int age; float salary; };


2 Answers

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
    };
}
like image 130
Alok Save Avatar answered Nov 25 '22 05:11

Alok Save


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.

like image 25
Nawaz Avatar answered Nov 25 '22 04:11

Nawaz