#define macro vs Tiny functions
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: #define macro vs Tiny functions

  1. #1
    Join Date
    Apr 2008
    Posts
    159

    #define macro vs Tiny functions

    I just Read #define macros use instead of tiny functions...How i rewrite the following tiny fn:
    by a #define macro

    Code:
    static long Multiply(long m,long n)
    {
        return ( ((long long) m) * ((long long) n) ) >> 32; 
    }
    Plz comment....

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,274

    Re: #define macro vs Tiny functions

    Read Stroustrup's answer to the FAQ: So, what's wrong with using macros?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: #define macro vs Tiny functions

    Also see this FAQ: http://www.codeguru.com/forum/showthread.php?t=328273.

    The worst part with macros is that being expanded at pre-compile time they are not helpful at all during debugging.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,721

    Re: #define macro vs Tiny functions

    A demonstration of the perils of macro 'functions'
    Try compiling this...
    Code:
    #define Multiply(a, b) ((a) * (b))
    
    struct Test
    {
        int Multiply(int a)
        {
            return (a * 2);
        }
    };
    
    int main()
    {
        Test test;
    
        int result = test.Multiply(3);
    
        return 0;
    }
    Then try this...
    Code:
    int Multiply(int a, int b)
    {
        return (a * b);
    }
    
    struct Test
    {
        int Multiply(int a)
        {
            return (a * 2);
        }
    };
    
    int main()
    {
        Test test;
    
        int result = test.Multiply(3);
    
        return 0;
    }
    If you really want macro style flexibility...
    Code:
    template <typename TReturn, typename T1, typename T2>
    TReturn Multiply(T1 a, T2 b)
    {
        return static_cast<TReturn>(a * b);
    }
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center