scope of struct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Szabolcs Borsanyi

    #1

    scope of struct

    Dear all,

    I'd like to define a macro that expands to a struct-or-union-specifier.
    Inside of this struct there shall be a named structure. Alas, the struct
    tag inside the other unnamed structure has the scope of the translation unit,
    is that right?
    So how could I possibly modify the following simplified code to bring it
    back within the language constraints.

    #define type_template(t ypename) \
    struct { int a; struct a { struct a *p; typename x; } b; }

    int main()
    {
    type_template(d ouble) A;
    type_template(d ouble) B;
    type_template(i nt*) C;
    return 0;
    }

    I have one idea, but that will break if I erase the newline after A;

    Thanks,
    Szabolcs
  • Richard Tobin

    #2
    Re: scope of struct

    In article <20080527092208 .GA26191@kroto. pact.cpes.susx. ac.uk>,
    Szabolcs Borsanyi <s.borsanyi@sus sex.ac.ukwrote:
    >I'd like to define a macro that expands to a struct-or-union-specifier.
    >Inside of this struct there shall be a named structure. Alas, the struct
    >tag inside the other unnamed structure has the scope of the translation unit,
    >is that right?
    For the declarations inside main(), the scope will be main(), but you
    still have the same problem.

    The problem is that C doesn't have a "gensym" to create new
    names at compile time. Presumably you were thinking of using
    __LINE__.

    It's a bit less elegant but you could have a two-argument macro that
    typedefs a struct, the arguments being the type to base it on and the
    name of the new type, and construct a name for the internal struct
    out of the new type name using ##.

    -- Richard
    --
    In the selection of the two characters immediately succeeding the numeral 9,
    consideration shall be given to their replacement by the graphics 10 and 11 to
    facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

    Comment

    • Barry Schwarz

      #3
      Re: scope of struct

      On Tue, 27 May 2008 09:22:08 +0000 (UTC), Szabolcs Borsanyi
      <s.borsanyi@sus sex.ac.ukwrote:
      >Dear all,
      >
      >I'd like to define a macro that expands to a struct-or-union-specifier.
      >Inside of this struct there shall be a named structure. Alas, the struct
      >tag inside the other unnamed structure has the scope of the translation unit,
      >is that right?
      >So how could I possibly modify the following simplified code to bring it
      >back within the language constraints.
      >
      >#define type_template(t ypename) \
      You could always use
      #define type_template(t ypename, struct_tag) \
      struct { int a; struct a { struct a *p; typename x; } b; }
      and change this to
      struct { int a;
      struct struct_tag { struct struct_tag *p;
      typename x; } b;
      }
      >
      >int main()
      >{
      type_template(d ouble) A;
      and these to
      type_template(d ouble, a1) A;
      type_template(d ouble) B;
      and
      type_template(d ouble, a2) B;
      etc.
      type_template(i nt*) C;
      return 0;
      >}
      >

      Remove del for email

      Comment

      • Hallvard B Furuseth

        #4
        Re: scope of struct

        Szabolcs Borsanyi writes:
        I'd like to define a macro that expands to a struct-or-union-specifier.
        Inside of this struct there shall be a named structure.
        You are sure it has to be named? If you need the name e.g. for
        offsetof, it usually works to say offsetof(struct <outer>,
        <outer member>.<inner member>). Though I don't think that's valid C.
        Alas, the struct tag inside the other unnamed structure has the scope
        of the translation unit, is that right?
        Same scope as the outer struct. Though if you switch to C++, the struct
        name there is struct <outer struct name>::<inner struct name>.
        (...)
        I have one idea, but that will break if I erase the newline after A;
        There may be compilers where that also breaks in a macro defining
        several such structs separated by backslash-newline. I'm not sure if
        it'd be a compiler bug to remove backslash-newline before expanding
        __LINE__.

        --
        Hallvard

        Comment

        • Barry Schwarz

          #5
          Re: scope of struct

          On Tue, 27 May 2008 17:43:55 +0200, Hallvard B Furuseth
          <h.b.furuseth@u sit.uio.nowrote :
          >Szabolcs Borsanyi writes:
          >I'd like to define a macro that expands to a struct-or-union-specifier.
          >Inside of this struct there shall be a named structure.
          >
          >You are sure it has to be named? If you need the name e.g. for
          >offsetof, it usually works to say offsetof(struct <outer>,
          ><outer member>.<inner member>). Though I don't think that's valid C.
          Since his internal structure has a pointer to itself, the tag does
          seem essential.
          >
          >Alas, the struct tag inside the other unnamed structure has the scope
          >of the translation unit, is that right?
          >
          >Same scope as the outer struct. Though if you switch to C++, the struct
          >name there is struct <outer struct name>::<inner struct name>.
          >
          >(...)
          >I have one idea, but that will break if I erase the newline after A;
          >
          >There may be compilers where that also breaks in a macro defining
          >several such structs separated by backslash-newline. I'm not sure if
          >it'd be a compiler bug to remove backslash-newline before expanding
          >__LINE__.

          Remove del for email

          Comment

          • Peter Nilsson

            #6
            Re: scope of struct

            Szabolcs Borsanyi <s.borsa...@sus sex.ac.ukwrote:
            ... how could I possibly modify the following simplified
            code to bring it back within the language constraints.
            >
            #define type_template(t ypename) \
            struct { int a; struct a { struct a *p; typename
            x; } b; }
            >
            int main()
            {
            type_template(d ouble) A;
            type_template(d ouble) B;
            type_template(i nt*) C;
            return 0;
            }
            >
            I have one idea, but that will break if I erase the
            newline after A;
            #define CAT(a,b) a ## b
            #define CAT2(a,b) CAT(a,b)

            #define declare_struct_ outer(typename) \
            struct CAT2(inner_, typename) \
            { \
            struct CAT2(inner_, typename) *p; \
            typename x; \
            }; \
            struct CAT2(outer_, typename) \
            { \
            int a; \
            struct CAT2(inner_, typename) b; \
            }

            typedef int *int_ptr;

            declare_struct_ outer(double);
            declare_struct_ outer(int_ptr);

            int main()
            {
            struct outer_double A;
            struct outer_double B;
            struct outer_int_ptr C;
            return 0;
            }

            --
            Peter

            Comment

            • Hallvard B Furuseth

              #7
              Re: scope of struct

              Barry Schwarz <schwarzb@dqel. comwrites:
              On Tue, 27 May 2008 17:43:55 +0200, Hallvard B Furuseth
              >>You are sure it has to be named? (...)
              >
              Since his internal structure has a pointer to itself, the tag does
              seem essential.
              Duh, of course.

              --
              Hallvard

              Comment

              Working...