英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论