Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #8053

Re: #define - in header or cpp

From Victor Bazarov <v.bazarov@comcast.invalid>
Newsgroups comp.lang.c++
Subject Re: #define - in header or cpp
Date 2011-07-21 15:21 -0400
Organization A noiseless patient Spider
Message-ID <j09u72$cp5$1@dont-email.me> (permalink)
References <d0a4638c-2b91-4099-8718-cb47ada19e53@h14g2000yqd.googlegroups.com> <j09gu1$10qq$1@news.ett.com.ua> <e0b93$4e28719b$506c0d9c$16489@news.chello.at>

Show all headers | View raw


On 7/21/2011 2:36 PM, Paul Brettschneider wrote:
> Balog Pal wrote:
>
>> 2. #define is just for last resort -- use C++ native stuff wherever
>> possible.
>
> Somewhat off-topic: What is the C++-way of implementing string literal
> macros for use with concetenation?

It's a simple text replacement.  There are no "string literal macros".

>
> What I mean is this:
>
> #define CONTROL_SEQUENCE "\xde\xad\xbe\xef"
> static const char control_sequence[] = "\xde\xad\xbe\xef";
> static const char *f1()
> {
>          return "Test " CONTROL_SEQUENCE " string"; // Compiles but ugly

This is converted by the preprocessor into

return "Test " "\xde\xad\xbe\xef" " string";

(whitespace condenced, comments replaced with whitespace, macros are 
replaced with their "values" - straight text substitution), and then the 
adjacent string literals are concatenated (phase 6), so you get

return "Test \xde\xad\xbe\xef string";

just before the compiler gets that list of *tokens*.

> }
> static const char *f2()
> {
>          return "Test " control_sequence " string"; // Error
> }

There is no textual replacement here.  It's a different list of tokens 
the compiler gets:

return "Test " control_sequence " string";

in which no string literal appears in place of 'control_sequence' (which 
is a variable name), and hence no concatenation occurs, and it's just a 
syntax error after that.

Read about compilation stages in section 2.1 of the Standard.  Macro 
substitution is also described in section 16.3.

V
-- 
I do not respond to top-posted replies, please don't ask

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

#define - in header or cpp Christopher <cpisz@austin.rr.com> - 2011-07-21 08:11 -0700
  Re: #define - in header or cpp Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-21 11:27 -0400
  Re: #define - in header or cpp "Balog Pal" <pasa@lib.hu> - 2011-07-21 17:34 +0200
    Re: #define - in header or cpp Christopher <cpisz@austin.rr.com> - 2011-07-21 10:42 -0700
    Re: #define - in header or cpp Paul Brettschneider <paul.brettschneider@yahoo.fr> - 2011-07-21 20:36 +0200
      Re: #define - in header or cpp Victor Bazarov <v.bazarov@comcast.invalid> - 2011-07-21 15:21 -0400
        Re: #define - in header or cpp Paul Brettschneider <paul.brettschneider@yahoo.fr> - 2011-07-21 23:27 +0200
      Re: #define - in header or cpp Ian Collins <ian-news@hotmail.com> - 2011-07-22 09:35 +1200
        Re: #define - in header or cpp Paul Brettschneider <paul.brettschneider@yahoo.fr> - 2011-07-21 23:57 +0200
        Re: #define - in header or cpp Thomas Boell <tboell@domain.invalid> - 2011-07-22 01:06 +0200
          Re: #define - in header or cpp Ian Collins <ian-news@hotmail.com> - 2011-07-22 11:14 +1200
          Re: #define - in header or cpp Paul Brettschneider <paul.brettschneider@yahoo.fr> - 2011-07-22 08:49 +0200
            Re: #define - in header or cpp Joe Greer <jgreer@doubletake.com> - 2011-07-22 13:23 +0000
            Re: #define - in header or cpp James Kanze <james.kanze@gmail.com> - 2011-07-22 12:58 -0700
    Re: #define - in header or cpp James Kanze <james.kanze@gmail.com> - 2011-07-22 12:51 -0700
  Re: #define - in header or cpp gwowen <gwowen@gmail.com> - 2011-07-22 00:29 -0700
    Re: #define - in header or cpp Christopher <cpisz@austin.rr.com> - 2011-07-22 15:56 -0700
  Re: #define - in header or cpp Juha Nieminen <nospam@thanks.invalid> - 2011-07-22 14:33 +0000
    Re: #define - in header or cpp Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-07-26 11:26 +0000
      Re: #define - in header or cpp Juha Nieminen <nospam@thanks.invalid> - 2011-07-26 16:14 +0000

csiph-web