Have macro 'return' a value

后端 未结 9 1871
闹比i
闹比i 2021-01-31 10:12

I\'m using a macro and I think it works fine -

#define CStrNullLastNL(str) {char* nl=strrchr(str,\'\\n\'); if(nl){*nl=0;}}

So it works to zero out the last

9条回答
  •  佛祖请我去吃肉
    2021-01-31 10:35

    If you don't have a strict requirement to use only macro, you can do something like this (real life example):

    #define Q_XCB_SEND_EVENT_ALIGNED(T) \
        q_xcb_send_event_aligned()
    
    template inline
    T q_xcb_send_event_aligned()
    {
        union {
            T event;
            char padding[32];
        } event;
    
        memset(&event, 0, sizeof(event));
        return event.event;
    }
    

    And then use it in your code like this:

    auto event = Q_XCB_SEND_EVENT_ALIGNED(xcb_unmap_notify_event_t);
    

提交回复
热议问题