Jump to content

  • Log In with Google      Sign In   
  • Create Account

14 years ago on June 15th Gamedev.net was first launched! We want to thank all of you for being part of our community and hope the best years are ahead of us. Happy birthday Gamedev.net!

#ActualHodgman

Posted 21 March 2013 - 11:16 PM

Does it matter?

Inline functions are preferred over macros in most C++ style guides, and should only be used where absolutely necessary.

In the case of safe_delete, an inline function is just as applicable, which means a macro isn't necessary.

 

template<class T> void safe_delete(T* x) { delete x; x = NULL; }

BTW, the if isn't necessary, as delete NULL; is valid (it does nothing) -- the if statement is often put there by programmers who learned on MSVC 6.0, which implemented "Microsoft C++", rather than C++98.

 

The main evilness about macros is that they don't obey C++ name scoping rules, so they can inadvertantly break other code.

e.g. say that I buy your code as a bit of middleware, and #include it into my own code that follows:

 

#include "cool_middleware.h"
namespace hodgman
{
  template<class T> void safe_delete(T* x) { if(x) x->Release; }
 
  void Frobnicate(Foo* foo)
  {
    foo->DoStuff();
    safe_delete(foo);
  }
}

This as soon as I #include your code in this file, my code breaks because of the macro! It becomes quite different:

 

 
  void Frobnicate(Foo* foo)
  {
    foo->DoStuff();
    if(foo) { delete foo; foo = NULL; }
  }

If you'd used an inline function for safe_delete, then everything would be fine

 

Further reading:

http://www.parashift.com/c++-faq/inline-vs-macros.html

http://www.parashift.com/c++-faq/macros-with-if.html

http://www.parashift.com/c++-faq/macros-with-multi-stmts.html

http://www.parashift.com/c++-faq/macros-with-token-pasting.html


#1Hodgman

Posted 21 March 2013 - 10:36 PM

Does it matter?

Inline functions are preferred over macros in most C++ style guides, and should only be used where absolutely necessary.

In the case of safe_delete, an inline function is just as applicable, which means a macro isn't necessary.

template<class T> void safe_delete(T* x) { delete x; x = NULL; }

BTW, the if isn't necessary, as delete NULL; is valid (it does nothing) -- the if statement is often put there by programmers who learned on MSVC 6.0, which implemented "Microsoft C++", rather than C++98.

 

The main evilness about macros is that they don't obey C++ name scoping rules, so they can inadvertantly break other code.

e.g. say that I buy your code as a bit of middleware, and #include it into my own code that follows:

#include "cool_middleware.h"
namespace hodgman
{
  template<class T> void safe_delete(T* x) { if(x) x->Release; x = 0; }
 
  void Frobnicate(Foo* foo)
  {
    foo->DoStuff();
    safe_delete(foo);
  }
}

This as soon as I #include your code in this file, my code breaks because of the macro! It becomes quite different:

 
  void Frobnicate(Foo* foo)
  {
    foo->DoStuff();
    if(foo) { delete foo; foo = NULL; }
  }
If you'd used an inline function for safe_delete, then everything would be fine

 

Further reading:

http://www.parashift.com/c++-faq/inline-vs-macros.html

http://www.parashift.com/c++-faq/macros-with-if.html

http://www.parashift.com/c++-faq/macros-with-multi-stmts.html

http://www.parashift.com/c++-faq/macros-with-token-pasting.html


PARTNERS