Functions as "#define"

This is a discussion on Functions as "#define" within the C++ Programming forums, part of the General Programming Boards category; Hello, I found this code on the internet: Code: #define _LPM(addr) \ ({ \ UInt16 __addr16 = (UInt16)(addr); \ UInt8 ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    106

    Functions as "#define"

    Hello, I found this code on the internet:
    Code:
    #define _LPM(addr)   \
    ({                              \
        UInt16 __addr16 = (UInt16)(addr); \
        UInt8 __result;           \
        __asm__                     \
        (                           \
            "lpm" "\n\t"            \
            "mov %0, r0" "\n\t"     \
            : "=r" (__result)       \
            : "z" (__addr16)        \
            : "r0"                  \
        );                          \
        __result;                   \
    })
    I need to understand the use of the braces here: does {.....} evaluate to something when I write, for example, UInt8 val = _LPM( addr ) ?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    That code, as it stands, is not a valid macro in C or C++. It is either for another language, or is exploiting some extensions that are specific to a particular compiler.

    It might also be input (eg in some scripting language) to some other program that generates C/C++ code from that input.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    Looking around I found that the question could be better expressed in this words: how does c++ evaluate compound statements( same as "blocks")? Does blocks evaluate to something (an address, an integer....something)? To me, the answer is no, but I wonder why this code I wrote up works...I use a compiler for a ATMEL microcontroller to compile this: ATMEL does use HARVARD architecture, so the constants and program memory is not accessible directly, but must be accessed using particular assembly instructions as "lpm", and this macro does the work, retrieving a byte from program memory.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    This is a GCC extension. The result of the block is the result of the last statement, i.e. __result in this case.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    106
    Quote Originally Posted by CornedBee View Post
    This is a GCC extension. The result of the block is the result of the last statement, i.e. __result in this case.
    So I'm not going to expect that VisualC++ supports it....:P

    Thank you for the explanations ^___^

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No, definitely not. Even if it supported the block-as-expression extension, the inline assembly syntax is GCC's too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21