令牌粘贴通用结构

Jac*_*k H 3 c structure c-preprocessor

我试图使用令牌粘贴在C中定义一些简单的通用结构.其中一个例子如下: -

#define VECTOR(type, dimension, name) \
    typedef struct Vector_##type{ \
        type data[##dimension##]; \
        int dim = dimension; \
    }##name##;
VECTOR(int, 3, Vector3i)
Run Code Online (Sandbox Code Playgroud)

根据我的理解,由于嵌入了参数,第2和第5行必须进行标记粘贴,而出于相反的原因,第4行不需要标记粘贴.

但是,由于上面的简单示例导致以下错误,我似乎在我的理解中遗漏了一些内容: -

pasting formed '[3', an invalid preprocessing token VECTOR(int, 3, Vector3i)

pasting formed '3]', an invalid preprocessing token VECTOR(int, 3, Vector3i)

expected member name or ';' after declaration specifiers
VECTOR(int, 3, Vector3i)
~~~~~~~~~~~~^~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

鉴于上述错误,我错过了什么,以及在纯C中定义通用结构的适当方法是什么?

Som*_*ude 5

您不需要对大小或类型名称进行令牌粘贴操作.

它用于实际从多个创建单个令牌,因此Vector_##type创建单个Vector_int令牌是有意义的.

[##dim##]尝试创建单个 [3]令牌的位置没有意义.这应该是三个不同的和单独的标记:[后面3跟着][dim]在宏中使用.

它与typedef结构后面的名称相同.