默认函数参数和单一定义规则

huangapple go评论65阅读模式
英文:

Default function arguments and the One Definition Rule

问题

我问这个问题是因为我一直没有找到确切的谷歌方法。

问题是,如果我在库中有这样的内容:

#ifdef PROVIDE_DEFAULT_ARG
#define DEFAULT_ARG(v) = v
#else
#define DEFAULT_ARG()
#endif

void Foo(int a, int b DEFAULT_ARG(1));

而且库在未定义PROVIDE_DEFAULT_ARG的情况下编译,然后使用该库的程序在定义了它的情况下进行编译(或者反之),是否违反了单一定义规则(One Definition Rule)?

英文:

I'm asking this question because I haven't had any luck figuring out exactly how to google for it.

The question is, if I have something like this in a library:

#ifdef PROVIDE_DEFAULT_ARG
#define DEFAULT_ARG(v) = v
#else
#define DEFAULT_ARG()
#endif

void Foo(int a, int b DEFAULT_ARG(1));

And the library is compiled without defining PROVIDE_DEFAULT_ARG, and then the program using the library is compiled with it defined (or vice versa), does this violate the One Definition Rule?

答案1

得分: 2

ODR仅在您有多个定义时适用。您只有一个默认参数的定义:在使用该库的程序中。它不可能与该库的翻译单元发生冲突,因为该单元没有默认参数的任何定义。

英文:

The ODR only applies when you have more than one definition. You only have one definition of the default argument: in the program using the library. It cannot possibly conflict with the library's translation unit which doesn't have any definition for the default argument.

huangapple
  • 本文由 发表于 2023年4月20日 07:24:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定