1

In the beginning of my engeenering school, my teachers introduced me to a particular use of macros in C++ : when declaring template classes, they told me to create a macro that abstracts the template parameters of methods definitions.

For example, if i have a class named MyList which is a list of element of type T, and i have a method :

template<typename T>
void MyList<T>::add(T element);

They encouraged us to create the following macros in the cpp file :

define TEMPL template<typename T>
define MY_LIST MyList<T>

So as the method definition becomes :

TEMPL void MY_LIST::add(T element);

Hence, if i decide to change the template parameters, i only have to change the macro definition. Do you also think that this is a good practice? Recently i also started to use it to abstract the namespace in method definitions, would you recommend it? And if you wouldn't, why?

I have also another problem with this practice, it is that Visual Studio does not recognize the methods definitions in the cpp file due to the use of the macro. In the header file, the methods declarations are underlined with a message 'Function definition not found', despite the code compile. If it is a good practice, is there a way to remove these warnings?

Thanks you in advance.

Ablil
  • 11
  • 3
  • 6
    *"They encouraged us to create the following macros in the cpp file"*. What ?! :-( – Jarod42 Apr 23 '21 at 07:44
  • 7
    *"Do you also think that this is a good practice?"*. No. – Jarod42 Apr 23 '21 at 07:46
  • 2
    BTW, see [why-can-templates-only-be-implemented-in-the-header-file](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – Jarod42 Apr 23 '21 at 07:47
  • 4
    This is pretty horrendous advice. Yeah, sure, if you make it a macro then if you happen to change the template parameters then you only need to do it in one place. But basically nobody does this. You know why? Because we have modern text editors with search-and-replace. – paddy Apr 23 '21 at 07:47
  • 3
    Another vote for nope, macros have very limited uses in modern c++, this isn't one of them. You're executively changing the syntax of the language, any newcomer to your code would have to work out what your macros are before being able to understand the code – Alan Birtles Apr 23 '21 at 07:47
  • One more detail : the cpp file in which they told me to add the macro is a cpp file included from a header file in order to avoid declaring the templated methods in the header file. Maybe does it change something? – Ablil Apr 23 '21 at 07:48
  • 2
    Not really, you shouldn't be including cpp files either – Alan Birtles Apr 23 '21 at 07:50
  • 1
    So I will add, don't include cpp files (rename it to .hxx, or .tcc, .inc) – Jarod42 Apr 23 '21 at 07:50
  • @Ablil are you saying that you have a `#include "somefile.cpp"` in a header file? – Jabberwocky Apr 23 '21 at 07:50
  • Thanks your for your replies, i think i understand the problem. In fact, it is better to implement the templated methods in the header file. Is this correct? – Ablil Apr 23 '21 at 07:50
  • 2
    @Jabberwocky Yes, but i just remembered that they asked us to name it .hxx. – Ablil Apr 23 '21 at 07:51
  • There may be some usecase where this makes sense, but as a general rule I find it horrible. :) – Devolus Apr 23 '21 at 07:53
  • 1
    Yeah, AND don't use macros like this. Macros might be acceptable in some special cases where you actually have another code-generation step that creates many source files from a "template". But that's a stretch, and this is really not what you're asking. Don't forget that you basically lose ALL "benefits" of this macro if your type name is changed to something other than `T`. And that's a simple and not-so-uncommon example. By hiding your definitions inside a macro, the only thing you get is code that's less readable. – paddy Apr 23 '21 at 07:53
  • 1
    @paddy Ok thank you, i understood. Basically this practice don't bring many advantages and also bring several disavantages. So i shouldn't follow it. – Ablil Apr 23 '21 at 07:57
  • 6
    Feel free to tell your instructors that Stack Overflow rejects their advice. – paddy Apr 23 '21 at 07:58
  • 1
    A note why macros are considered as bad choice in C++: Modern C++ makes extensive use of namespaces. The pre-processor (which was invented decades earlier) doesn't know about namespaces. Hence, a macro may be replaced surprisingly where it isn't expected, and this introduces hard to fix errors (as usually nobody cares to look at the pre-processed source which is a pain btw.) The fact that templates have to be implemented in headers (in most cases) makes it worse. Hence, at best, macros shouldn't be used where better alternatives are available (and in most cases, there are in C++). – Scheff's Cat Apr 23 '21 at 09:34

0 Answers0