-3

i want something like

#define TEXT: "something "
int main(){


}

99% example "lala" is a new instance of a class

user2926018
  • 11
  • 1
  • 6
  • 3
    What are you trying to do with this? As is, `TEXT(abc)` is equivalent to writing `: std::cout< – chris Dec 23 '15 at 01:54
  • I voted to clear this question, because it is still unclear .... What is the actual usecase and the motivation.... – Basile Starynkevitch Dec 26 '15 at 10:34
  • I have define something like TEXT(name): with the colon in the end because in main() should be TEXT(R1): .. with the colon in the end.. Maybe this can be solved with operator overloading? it is not clear if TEXT(r1): is new instance of a class or not.. – user2926018 Dec 26 '15 at 18:29

3 Answers3

5

Macro names should only consist of alphanumeric characters and underscores, i.e. 'a-z', 'A-Z', '0-9', and '_', and the first character should not be a digit. Some preprocessors also permit the dollar sign character '$', but you shouldn't use it; unfortunately I can't quote the C standard since I don't have a copy of it.

From the GCC documentation:

Preprocessing tokens fall into five broad classes: identifiers, preprocessing numbers, string literals, punctuators, and other. An identifier is the same as an identifier in C: any sequence of letters, digits, or underscores, which begins with a letter or underscore. Keywords of C have no significance to the preprocessor; they are ordinary identifiers. You can define a macro whose name is a keyword, for instance. The only identifier which can be considered a preprocessing keyword is defined. See Defined.

This is mostly true of other languages which use the C preprocessor. However, a few of the keywords of C++ are significant even in the preprocessor. See C++ Named Operators.

In the 1999 C standard, identifiers may contain letters which are not part of the “basic source character set”, at the implementation's discretion (such as accented Latin letters, Greek letters, or Chinese ideograms). This may be done with an extended character set, or the '\u' and '\U' escape sequences. The implementation of this feature in GCC is experimental; such characters are only accepted in the '\u' and '\U' forms and only if -fextended-identifiers is used.

As an extension, GCC treats '$' as a letter. This is for compatibility with some systems, such as VMS, where '$' is commonly used in system-defined function and object names. '$' is not a letter in strictly conforming mode, or if you specify the -$ option. See Invocation.

Nutan
  • 778
  • 1
  • 8
  • 18
2

Preprocessor identifiers can be any sequence of letters, digits, or underscores that start with a letter or underscore. In other words, you can't have one that ends with (or generally contains) a colon.

Turn
  • 6,656
  • 32
  • 41
-1

It is usually poor taste to define a macro ending with colon or semi-colon. Try instead

#define OUT(name) do {std::cout << name;} while(0)

See this explanation.

If it is some logging macro, consider instead

#define LOG(Out) do {std::cout << __FILE__ << ":" << __LINE__ << " " \
                     << Out << std::endl; } while(0)

and you could use it as: LOG("x=" << x << " and y=" << y);

BTW, you could ask your compiler to show you the result of the preprocessing. If using GCC you could compile your source code mysource.cc with g++ -C -E -H mysource.cc > mysource.ii (perhaps add other relevant preprocessor options, like -Isomedir, before the -H) and look with an editor or a pager into the generated mysource.ii which contains the preprocessed form.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • For me it looks like that OP wants macro *name* to end with colon. – HolyBlackCat Dec 26 '15 at 10:28
  • i want to call the macro TEXT(lala): with the colon in the end something like #define TEXT(name): "something **OR a new instance of class**" int main(){ TEXT(lala): } – user2926018 Dec 26 '15 at 18:33