Why is a level of indirection needed for this concatenation macro?

前端 未结 1 2032
别跟我提以往
别跟我提以往 2020-12-05 18:35

I found an interesting little blog post that explains how to generate (semi) unique names in a macro by using the line number:

// Do magic! Creates a unique          


        
1条回答
  •  自闭症患者
    2020-12-05 18:58

    Why does the LINE_NAME macro even work if JOIN is declared after it in the file?

    Macros are not functions, when you call them compiler expands them, and there compiler at the using point knows about all defined macros.

    Why is it necessary to use both the JOIN and _DO_JOIN macros in order to get the correct result? Having this level of indirection in the macros seems very strange.

    Because __LINE__ itself is a macro, it needs two level expanding.

    Otherwise the output is not prefix1234 it will be prefix__LINE__.

    it's useful to read this answer too, and this thread.

    0 讨论(0)
提交回复
热议问题