简体   繁体   English

宏都将获取(void *)项的地址和长度,用于数组和结构

[英]Macro to both get (void*) address of item and length, for arrays and structs

I'm trying to design a macro to produce several related data structures related to things that need initialization. 我正在尝试设计一个宏,以产生与需要初始化的事物相关的几个相关数据结构。 The code has to compile under both C and C++. 该代码必须在C和C ++下都可以编译。 The goal is to have something like: 目的是要有类似的东西:

MUNGE_THING(struct1);
  MUNGE_THING(array1);

turn into something equivalent to 变成等同于

munge_thing((void*)&struct1, sizeof(struct1));
  munge_thing((void*)array1, sizeof(array1));

Is there any syntactic stuff I can surround the macro argument with so that it will handle both arrays and structure correctly both when taking the address and when getting the size? 有什么语法上的东西可以包围宏参数,以便在获取地址和获取大小时都能正确处理数组和结构? The most likely context will be in the constant declaration of an initialization list. 最可能的上下文将在初始化列表的常量声明中。

If that isn't possible, and it's necessary to use separate macros for structures and arrays, what would be the best syntax to ensure that passing something incorrectly will yield a compile error rather than bogus code? 如果那是不可能的,并且有必要对结构和数组使用单独的宏,那么什么是最好的语法来确保错误地传递某些内容会产生编译错误而不是伪代码?

In "old" C, prepending an array address with "&" would yield a warning, but not prevent compilation. 在“旧” C中,在数组地址前加上“&”会产生警告,但不会阻止编译。 In C++, it seems to yield the address of a location which stores the address of the array. 在C ++中,似乎产生了存储数组地址的位置的地址。

The MUNGE_THING macros are going to be within another macro that will be invoked multiple times with different definitions of MUNGE_THING, so having separate macros for arrays and structs would be irksome. MUNGE_THING宏将位于另一个宏中,该宏将使用MUNGE_THING的不同定义多次调用,因此为数组和结构使用单独的宏会很麻烦。 The best approach I can figure would be to give MUNGE_THING an extra argument for the "optional" ampersand, but that somehow seems ugly. 我能想到的最好的方法是给MUNGE_THING一个额外的参数来表示“可选”的“&”号,但是这看起来很难看。

If the array is in fact an array (which seems to be required for the sizeof to work), why don't you just use the simple macro: 如果数组实际上是一个数组( sizeof要正常工作似乎是必需的),为什么不只使用简单的宏:

#define MUNGE_THING( x ) munge_thing((void*)&(x), sizeof(x))

That should work both for arrays and structs: 这应该适用于数组和结构:

int array[10];
assert( (void*)array == (void*)&array );

You have tagged the question as both C and C++, in C++ you can use templates and avoid the macros all together. 您已将问题标记为C和C ++,在C ++中,您可以使用模板,并且避免一起使用宏。

I'm not sure what problem you are having with &array1 . 我不确定&array1有什么问题。 This C++ worked exactly as expected (all values the same) 此C ++完全按预期工作(所有值均相同)

int main(int argc, char* argv[])
{
    int array1[10];

    printf("%x %x\n", array1, &array1);
    cout << array1 << " " << &array1 << endl;

    void* ptr1 = array1;
    void* ptr2 = &array1;

    printf("%x %x\n", ptr1, ptr2);
    cout << ptr1 << " " << ptr2 << endl;
    return 0;
}

Okay, I see my confusion. 好吧,我看到了我的困惑。 In C++, the type of &array is not compatible with the type of the array, and as the linked discussion notes, (&array)+1 is not the same as (array+1), but casting the unsubscripted pointers does in fact yield the proper results. 在C ++中,&array的类型与数组的类型不兼容,并且正如链接的讨论所指出的那样,(&array)+1与(array + 1)不同,但是强制转换未下标的指针确实会产生适当的结果。 The distinctions between arrays and pointers in C are very confusing. C语言中的数组和指针之间的区别非常令人困惑。 Thanks for the assistance. 感谢您的协助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM