C++ macro '##' doesn't work after '->' operator

后端 未结 3 1685
青春惊慌失措
青春惊慌失措 2021-01-01 11:47

I have a shared_ptr object x, which has get and set methods as follows:

x->a_value();
x->set_a_value();
x->b_value();
x->set_b_value();
         


        
3条回答
  •  心在旅途
    2021-01-01 12:28

    The preprocessor works on "tokens" - likes names and operators.

    The ## operator creates a new token by pasting smaller parts together. In the first example set_##type##_value becomes set_a_value, which is a valid token.

    In the second example ->##type##_value would become ->a_value, which is not a valid preprocessor token. It ought to be two tokens.

    If you just make the line x->type##_value(); it should work. You get the separate tokens x, ->, a_value, (, ), and ;.

提交回复
热议问题