creating keyword

Dec 25, 2014 at 1:15pm
is it possible to create own keyword in c++ ?
if yes, can you give me an idea?
i know about typedef but thats only for redefining the existing ones.
Dec 25, 2014 at 1:41pm
What do you mean by keyword. In C++ keyword is language base. If you add or delete one it will not be C++ anymore. Not standard one, anyway.

However this
i know about typedef but thats only for redefining the existing ones.
makes me think that you mean something else. As you cannot typedef a keyword.
Dec 25, 2014 at 2:40pm
What do you mean by keyword. In C++ keyword is language base

i want to create my own keyword. example const
i want to make my own constant keyword with same definition as const
If you add or delete one it will not be C++ anymore. Not standard one, anyway.

isee . youve got a point there


makes me think that you mean something else. As you cannot typedef a keyword. 

i see. only data types can use typedef
Dec 25, 2014 at 3:47pm
As mentioned above, you can't do that in C++. However, if you're interested in this kind of metaprogramming, you should definitely take a look at LISP-like [1] languages (I strongly recommend Clojure [2]). What makes these languages so good at metaprogramming is their syntax:

In C++, an expression like 1 + 2 is eventually transformed by the compiler into something like this:

1
2
3
   +  
  / \  
 1   2

That is, a tree with + as the root and 1 and 2 as children
(usually referred to as the syntax tree of the expression).

In LISPs, a big (and difficult) step of this transformation is skipped entirely, as you essentially directly
provide the syntax tree: (+ 1 2) (the first term is the root, and the rest of the terms are the children).
This, combined with the fact that data (lists) and code (syntax trees) share the same textual
representation, provide the strong foundation LISP's powerful macro system is built upon.

So, take a look at LISP (or better yet, Clojure) first, and if you like it and want to enjoy this
kind of metaprogramming in C++ too, take a look at ktg's L++: https://bitbucket.org/ktg/l

[1] http://en.wikipedia.org/wiki/Lisp_%28programming_language%29
[2] http://clojure.org/
Dec 25, 2014 at 5:57pm
C++ has no facility to do what you want.

You have three basic options:

1. Macros. A lot of what people look to do can be done with preprocessor macros.

2. Get a better (or additional) preprocessor. A lot of people like the m4 preprocessor with C and C++. There is no injunction against using more than one. For an example of something fairly impressive take a look at Qt's moc preprocessor.

3. Modify the compiler's front end. (That's the past that runs your source code into an abstract syntax tree, or AST, that the compiler uses to optimize your code then emit executable machine data.)

As a matter of curiosity (and also the chance to give you a more useful answer), what exactly are you trying to accomplish with this?
Dec 25, 2014 at 11:03pm
Couldn't the OP make a function that returns a pointer to a variable (for example a
const
)
Dec 26, 2014 at 1:17am
Well, considering 'const' -- there isn't much point in making a new reserved word unless it does something unique. If all you want to do is rename something, a #define will do:

1
2
3
4
5
6
#define unmodifiable const

int main()
{
   unmodifiable int n = 74;
   ...

What's more interesting is new control constructs.
Topic archived. No new replies allowed.