简体   繁体   English

预处理器定义与函数原型的使用

[英]usage of pre processor definition with function prototype

I have to deal with two header files declaring the same prototype with different type name (note that the 2 types are the same at the end)我必须处理两个声明具有不同类型名称的相同原型的头文件(请注意,最后两种类型是相同的)

One of them is protected with #ifndef directive其中之一受#ifndef 指令保护

I am considering adding a define to avoid the double declaration but have been told is not good practice to have the same name for a define definction and for a function name.我正在考虑添加一个定义来避免双重声明,但被告知对于定义定义和函数名称使用相同的名称并不是一个好习惯。

acg_header.h acg_header.h

    #ifndef ACos
    /* MATH::ACos/ */
    extern T_Float_user ACos(/* X/ */ T_Float_user X);
    #endif /* ACos */

manual_header.h manual_header.h

    #define ACos // suggested modification
    extern t_float ACos(const t_float X);

I would like to know if is is a correct solution regarding C implementation.我想知道是否是关于 C 实现的正确解决方案。

You may have any number of compatible declarations of the same function or object.您可能有任意数量的相同函数或对象的兼容声明。 It is not necessary or useful to play preprocessor games to avoid this situation.为了避免这种情况,玩预处理器游戏没有必要或有用。 This is why the standard approach is for each function or global variable to be declared in a (one) header file, and for each source that accesses or defines any function or object to include the appropriate header.这就是为什么标准方法是在(一个)头文件中声明每个函数或全局变量,并且每个访问或定义任何函数或对象的源都包含适当的头文件。 So, with respect to所以,关于

I have to deal with two header files declaring the same prototype with different type name我必须处理两个声明具有不同类型名称的相同原型的头文件

, no, you shouldn't have to deal with that. ,不,你不应该处理那个。 There should be only one header declaring each function.应该只有一个头文件来声明每个函数。 Nevertheless, if you do have two, and they declare the function compatibly, then you still have no problem, even if the declarations do not lexically match on account of the use of different -- but compatible -- type aliases.尽管如此,如果您确实有两个,并且它们声明函数兼容,那么您仍然没有问题,即使由于使用了不同但兼容的类型别名,声明在词法上不匹配。

If the declarations in your two header files are not compatible with each other, on the other hand, then you have a deeper problem.另一方面,如果您的两个头文件中的声明彼此不兼容,那么您就有更深层次的问题。 At most one of them can be compatible with the function definition, and you must not use any other.其中最多一个可以与函数定义兼容,你不能使用任何其他的。

I am considering adding a define to avoid the double declaration but have been told is not good practice to have the same name for a define definction and for a function name.我正在考虑添加一个定义来避免双重声明,但被告知对于定义定义和函数名称使用相同的名称并不是一个好习惯。

Again, your best bet is to skip this whole exercise, but if you choose to go ahead, then you need to understand that appearances in your code of an in-scope macro identifier will be replaced with the macro definition.同样,您最好的选择是跳过整个练习,但如果您选择继续,那么您需要了解在您的代码中出现的范围内宏标识符将被宏定义替换。 Thus, the result of preprocessing this:因此,预处理的结果是:

 #define ACos // suggested modification extern t_float ACos(const t_float X);

would be将是

extern t_float (const t_float X);

, because you've defined ACos to expand to an empty sequence. ,因为您已定义ACos以扩展为空序列。 The preprocessed result is invalid.预处理结果无效。 If you must follow this path, then define the macro to expand to its own name:如果必须遵循此路径,则定义宏以扩展为自己的名称:

#define ACos ACos

You will then be able to test it via #ifdef directives, but you will avoid causing the preprocessor to mangle your program.然后您将能够通过#ifdef指令对其进行测试,但您将避免导致预处理器破坏您的程序。 But again, really, don't do this at all.但同样,真的,根本不要这样做。

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

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