1

I'm working with templates, and it's getting kinda tedious to write "typename" multiple times. Is it possible to assign an alias to it? I understand that the below doesn't work, is there any other ways?

typedef typename tn;
Cuadrix
  • 433
  • 3
  • 10
  • 1
    It would be possible with MACRO... – Jarod42 Dec 06 '20 at 22:20
  • @Jarod42 Even though I'm not very fond of macros, that'll have to do for now – Cuadrix Dec 06 '20 at 22:22
  • 1
    If this is a situation where someone will be reading your code ever I would advise against that. You may be able to create some shortcut key in your IDE instead to avoid the extra typing. – drescherjm Dec 06 '20 at 22:22
  • Aliases are for types. `typename` is a keyword. – super Dec 06 '20 at 22:23
  • 1
    The reason it is possible with macro is that the preprocessor will put `typename` everywhere `tn` occurred. I also recommend against. I also don't recommend using macros at all, some of the reasons can be found [Why are macros evil](https://stackoverflow.com/questions/14041453/why-are-preprocessor-macros-evil-and-what-are-the-alternatives) – Tony Tannous Dec 06 '20 at 22:24
  • @TonyTannous So is there any other alternative ways for aliasing keywords besides a preprocessor macro? Would be pretty useful – Cuadrix Dec 06 '20 at 22:26
  • 1
    Not that I am aware of. You can also write `class` instead of `typename` – Tony Tannous Dec 06 '20 at 22:28
  • Bummer. I guess I'm gonna have to accept the macro answer by @Jarod42 – Cuadrix Dec 06 '20 at 22:31
  • 2
    Why do this? Anybody reading your code will have an extremely hard time. – Robert Andrzejuk Dec 06 '20 at 22:35

1 Answers1

3

You might use MACRO

#define tn typename
Jarod42
  • 203,559
  • 14
  • 181
  • 302