470,870 Members | 1,411 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

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(typename) \
struct { int a; struct a { struct a *p; typename x; } b; }

int main()
{
type_template(double) A;
type_template(double) B;
type_template(int*) C;
return 0;
}

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

Thanks,
Szabolcs
Jun 27 '08 #1
6 2933
In article <20********************@kroto.pact.cpes.susx.ac.uk >,
Szabolcs Borsanyi <s.********@sussex.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)
Jun 27 '08 #2
On Tue, 27 May 2008 09:22:08 +0000 (UTC), Szabolcs Borsanyi
<s.********@sussex.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(typename) \
You could always use
#define type_template(typename, 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(double) A;
and these to
type_template(double, a1) A;
type_template(double) B;
and
type_template(double, a2) B;
etc.
type_template(int*) C;
return 0;
}

Remove del for email
Jun 27 '08 #3
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
Jun 27 '08 #4
On Tue, 27 May 2008 17:43:55 +0200, Hallvard B Furuseth
<h.**********@usit.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
Jun 27 '08 #5
Szabolcs Borsanyi <s.borsa...@sussex.ac.ukwrote:
... how could I possibly modify the following simplified
code to bring it back within the language constraints.

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

int main()
{
type_template(double) A;
type_template(double) B;
type_template(int*) 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
Jun 27 '08 #6
Barry Schwarz <sc******@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
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Tom Plunket | last post: by
5 posts views Thread by pembed2003 | last post: by
165 posts views Thread by Dieter | last post: by
2 posts views Thread by James Brown | last post: by
5 posts views Thread by Steven T. Hatton | last post: by
2 posts views Thread by Laurent Deniau | last post: by
7 posts views Thread by David Mathog | last post: by
1 post views Thread by Giacomo Catenazzi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.