macros with arguments Q 
Author Message
 macros with arguments Q

Hi,

How can I put a macro argument inside a string? E.g.

#define PRINTSIZE(T) cout << "sizeof(T) = " << sizeof(T) << endl;

will not expand the first occurrence of T.

Thanks.

Wroot

Sent via Deja.com
http://www.*-*-*.com/



Mon, 30 Jun 2003 01:59:11 GMT  
 macros with arguments Q

Quote:

> Hi,

> How can I put a macro argument inside a string? E.g.

Macros are not the answer.
Quote:

> #define PRINTSIZE(T) cout << "sizeof(T) = " << sizeof(T) << endl;

This is a very dangerous macro, because T will be evaluated more than
once.

A template should do it:

template <typename T>
inline void show(const T& t)
{
    cout << "sizeof(T) = " << sizeof(t) << endl;

Quote:
}

--
Jim
To suppress Deja's product links, add this header:
x-no-productlinks: yes

Sent via Deja.com
http://www.deja.com/



Mon, 30 Jun 2003 02:11:03 GMT  
 macros with arguments Q

Quote:

> Hi,

> How can I put a macro argument inside a string? E.g.

> #define PRINTSIZE(T) cout << "sizeof(T) = " << sizeof(T) << endl;

> will not expand the first occurrence of T.

You should be able to pick it up from my assert() example which is C not C++ as
you have posted here in comp.lang.c for some reason. Sorry for the line
wrapping.

#   define assert(cond)                \
    do                                 \
    {                                  \
        char line[161];                \
        if (!(cond))                   \
        {                              \
            sprintf(line, "%s%s%u%s", __FILE__, "(", __LINE__, ") : assert:\n
"); \
            sprintf(line, #cond "\n"); \
            for (;;);                  \
        }                              \
    } while (0)

--
- Mark -> My e-mail can be found via the Organization field
--



Mon, 30 Jun 2003 02:18:26 GMT  
 macros with arguments Q

Quote:



> > #define PRINTSIZE(T) cout << "sizeof(T) = " << sizeof(T) << endl;
> This is a very dangerous macro, because T will be evaluated more than
> once.

This macro does not evaluate T even once.
--
"What is appropriate for the master is not appropriate for the novice.
 You must understand the Tao before transcending structure."
--The Tao of Programming


Mon, 30 Jun 2003 02:23:18 GMT  
 macros with arguments Q

Quote:



>> Hi,

>> How can I put a macro argument inside a string? E.g.
>Macros are not the answer.

>> #define PRINTSIZE(T) cout << "sizeof(T) = " << sizeof(T) << endl;
>This is a very dangerous macro, because T will be evaluated more than
>once.

It will be expanded twice, but only one expansion will be evaluated.
The other will be stringified:

    #define PRINTSIZE(T) \
        printf("sizeof (" #T ") = %lu\n", (unsigned long) sizeof(T))

Quote:
>A template should do it:

>template <typename T>
>inline void show(const T& t)
>{
>    cout << "sizeof(T) = " << sizeof(t) << endl;
>}

Unfortunately, in addition to being off topic in comp.lang.c, this
doesn't work for type name expressions. The reference parameter of the
template function must be bound to an object, which means that you
cannot write:

    show(char **);

whereas with the macro you can do this.



Mon, 30 Jun 2003 02:46:27 GMT  
 macros with arguments Q


Quote:


> > Hi,

> > How can I put a macro argument inside a string? E.g.
> Macros are not the answer.

> > #define PRINTSIZE(T) cout << "sizeof(T) = " << sizeof(T) << endl;
> This is a very dangerous macro, because T will be evaluated more than
> once.

> A template should do it:

> template <typename T>
> inline void show(const T& t)
> {
>     cout << "sizeof(T) = " << sizeof(t) << endl;
> }

cout << "sizeof(T) = ";

will print

sizeof(T)

instead of

sizeof(double) or whatever

Wroot

Sent via Deja.com
http://www.deja.com/



Mon, 30 Jun 2003 02:54:03 GMT  
 macros with arguments Q

Quote:

> It will be expanded twice, but only one expansion will be evaluated.
> The other will be stringified:

>     #define PRINTSIZE(T) \
>         printf("sizeof (" #T ") = %lu\n", (unsigned long) sizeof(T))

sizeof doesn't evaluate it's argument (it's not even clear how it could
if it's a typename.

Besides, he wanted iostreams not printf:

        #define PRINTSIZE(T)    cout << "sizeof (" #T ") = " << sizeof(T) << "\n";

No goofy cast required.



Mon, 30 Jun 2003 03:18:50 GMT  
 macros with arguments Q

Quote:

> Besides, he wanted iostreams not printf:

>    #define PRINTSIZE(T)    cout << "sizeof (" #T ") = " << sizeof(T) << "\n";

cout is off-topic in comp.lang.c.  If he wanted iostreams then he
shouldn't have posted to clc.
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield


Mon, 30 Jun 2003 03:21:36 GMT  
 macros with arguments Q

[snip]
Quote:
> will print

> sizeof(T)

> instead of

> sizeof(double) or whatever

Ah, sorry, I missed that one. OK, then try using the stringize
operation:

#define PRINTSIZE(T) cout << "sizeof(" #T ") = " << sizeof(T) << endl;

That oughta do it!

--
Jim
To suppress Deja's product links, add this header:
x-no-productlinks: yes

Sent via Deja.com
http://www.deja.com/



Mon, 30 Jun 2003 03:34:27 GMT  
 macros with arguments Q

Quote:



>> > #define PRINTSIZE(T) cout << "sizeof(T) = " << sizeof(T) << endl;
>> This is a very dangerous macro, because T will be evaluated more than
>> once.

>This macro does not evaluate T even once.

As a macro, the supplied line looks OK, but when this macro is
expanded, the result will not be valid C code.

As the macro body _looks_ to be C++ code (and comp.lang.c discusses C
only), I'll do two things:

1) Give a potential C replacement macro
   #define PRINTSIZE(T) printf("sizeof(T) = %ld\n",(long)sizeof(T));

2) Set followups to comp.lang.c++

Lew Pitcher
Information Technology Consultant
Toronto Dominion Bank Financial Group


(Opinions expressed are my own, not my employer's.)



Mon, 30 Jun 2003 04:07:40 GMT  
 macros with arguments Q



Quote:
> >How can I put a macro argument inside a string? E.g.

> >#define PRINTSIZE(T) cout << "sizeof(T) = " << sizeof(T) << endl;

> Why do you post that to CLC? It's not C.

Because it was basically a macros question: how to get the macro
argument inside ""'s, namely "sizeof(T)"
(the answer I was looking for was " #T ")

Everyone, thanks for your help.

Wroot

Sent via Deja.com
http://www.deja.com/



Mon, 30 Jun 2003 08:01:06 GMT  
 macros with arguments Q

Quote:
>> >#define PRINTSIZE(T) cout << "sizeof(T) = " << sizeof(T) << endl;

>> Why do you post that to CLC? It's not C.

>Because it was basically a macros question: how to get the macro
>argument inside ""'s, namely "sizeof(T)"
>(the answer I was looking for was " #T ")

And what makes you think that the behaviour of the preprocessor is the same
in C and C++ ? There are different languages. Is your answer valid for C,
C++ or both?

--
-hs-    Tabs out, spaces in.
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://www.dinkum.com/htm_cl
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Mon, 30 Jun 2003 08:29:27 GMT  
 macros with arguments Q

Quote:


> >> >#define PRINTSIZE(T) cout << "sizeof(T) = " << sizeof(T) << endl;

> >> Why do you post that to CLC? It's not C.

> >Because it was basically a macros question: how to get the macro
> >argument inside ""'s, namely "sizeof(T)"
> >(the answer I was looking for was " #T ")

> And what makes you think that the behaviour of the preprocessor is the same
> in C and C++ ? There are different languages. Is your answer valid for C,
> C++ or both?

The preprocessor works the same way in C++ and C with respect to substitution.

The only difference is:

C90 doesn't support // style comments (C++ and C99 do).
All three differ slightly in the predefined macro names.

Despite the nit picking on the tag line of my post (which was really not
the issue), in neither C or C++ does the value substituted for T get
evalutated, since the only place it is used is for the stringify (which
just turns the value into a string literal) or because it is the arg of
sizeof.  It's not even clear what "evluate" means in the case where the
substituted value is a type name not an expression.



Mon, 30 Jun 2003 22:24:09 GMT  
 macros with arguments Q

Quote:


> > Besides, he wanted iostreams not printf:

> >       #define PRINTSIZE(T)    cout << "sizeof (" #T ") = " <<
sizeof(T) << "\n";

> cout is off-topic in comp.lang.c.  If he wanted iostreams then he
> shouldn't have posted to clc.

Have you not noticed the cross-posting? Have you not considered the fact
that the macro itself is common to both C and C++?

So it's using a freakin' C++ specific function - the point of the
question was not "How do I do output?" the point was "How do I print out
the identifier's type".

You and all others who've {*filter*}ed about "off-topic" should just get off
your {*filter*}y high horse and focus on what is important.

--
Jim
To suppress Deja's product links, add this header:
x-no-productlinks: yes

Sent via Deja.com
http://www.*-*-*.com/



Mon, 30 Jun 2003 23:50:45 GMT  
 macros with arguments Q

Quote:




> > > Besides, he wanted iostreams not printf:

> > >     #define PRINTSIZE(T)    cout << "sizeof (" #T ") = " <<
> sizeof(T) << "\n";

> > cout is off-topic in comp.lang.c.  If he wanted iostreams then he
> > shouldn't have posted to clc.
> Have you not noticed the cross-posting?

He may not have done. It's easily missed, sometimes. I recall a recent
crossposted thread where I had composed perhaps 80% of a reply to a very
large article before discovering that it was crossposted to
comp.lang.c++. I had to go back and edit the reply quite considerably
(otherwise the C++ guys would have been somewhat aggravated).

Quote:
> Have you not considered the fact
> that the macro itself is common to both C and C++?

Only someone well-versed in both languages could know that ***for
sure***, and some people understandably believe that it's better to be
safe than sorry.

Quote:

> So it's using a freakin' C++ specific function - the point of the
> question was not "How do I do output?" the point was "How do I print out
> the identifier's type".

> You and all others who've {*filter*}ed about "off-topic" should just get off
> your {*filter*}y high horse and focus on what is important.

Well, topicality is important, as both groups (I believe) realise. I
think comp.lang.c and comp.lang.c++ generally get along quite well with
each other in articles cross-posted to both groups, and in my experience
they tend to cut each other a little topical slack. This thread seems to
have been an unfortunate exception, possibly because the fact of the
crossposting may have been overlooked.

--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.*-*-*.com/ ~scs/C-faq/top.html
K&R Answers: http://www.*-*-*.com/



Tue, 01 Jul 2003 01:40:56 GMT  
 
 [ 18 post ]  Go to page: [1] [2]

 Relevant Pages 

1. wrong number of arguments to a macro?!

2. variable argument macros

3. Macro with variable arguments

4. wrong number of arguments to a macro?!

5. Variable number of arguments in macros

6. How to transfer a Argument in the macro

7. Macros with variable number of arguments?

8. Quote as argument to macro

9. macro with variable arguments

10. Using dynamic number of arguments in a macro

11. Variable number of arguments in a macro

12. Pointer type as Macro Argument

 

 
Powered by phpBB® Forum Software