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 6 3130
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)
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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Tom Plunket |
last post by:
I'm having some scoping issues, relating to the fact that
different compilers do different things. Who's doing the right
thing here?
class MyClass
{
public:
void Method();
private:
|
by: Ferdi Smit |
last post by:
I've never understood the rationale of allowing partial, but not
explicit specialization for classes at non-namespace scope. Ie.:
struct A {
template <typename T1, typename T2>
struct B {};
...
|
by: pembed2003 |
last post by:
Hi all,
I am reading the book "C How to Program" and in the chapter where it
discuss scope rule, it says there are four scopes for a variable:
function scope
file scope
block scope...
|
by: Dieter |
last post by:
Hi.
In the snippet of code below, I'm trying to understand why when the
struct dirent ** namelist
is declared with "file" scope, I don't have a problem freeing the
allocated memory. But...
|
by: James Brown |
last post by:
All,
Could anyone explain why the following is an error:
void foo( struct FOO { int x; } f1 )
{
}
int main()
|
by: Steven T. Hatton |
last post by:
This note appears in the discussion of name hiding and uniqueness:
§3.3 #4
This note is item #6 in the discussion of "Point of declaration"
§3.3.1 #6
What exactly do these statements mean?...
|
by: psroga |
last post by:
Can anyone look at this code and let me know why pthread_mutex_unlock
and pthread_mutex_lock are giving me the "phtread_mutex_unlock" was
not defined in this scope error.
I am compiling the code...
|
by: Laurent Deniau |
last post by:
I would like to know why the following small program does not compile
(checked with gcc 4.1.2) and if the compiler behavior is correct:
struct A;
typedef void (T)(struct A*);
void f(void) {...
|
by: David Mathog |
last post by:
I accidentally did this the other day (it was a lot less obvious in the
much longer actual program, hundreds of lines are omitted):
----------------------------------------------------------...
|
by: Giacomo Catenazzi |
last post by:
Hello,
To learn the details of C, I've build the following example,
could you check if it is correct and if it miss some important
cases?
Are there some useful (real cases) examples of:
-...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |