fix paddle namespace conflict when using paddle_flags #56913
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR types
Others
PR changes
Others
Description
上述问题的原因在
paddle::xxx
命名空间下定义或者声明了 FLAG,比如:Paddle/paddle/fluid/distributed/ps/service/heter_client.cc
Lines 20 to 23 in e2af9d5
在对该文件进行宏展开预处理后,会得到:
相当于在
paddle::distributed
命名空间下又定义了一个paddle
命名空间,这样的话后续该文件在paddle::distributed
命名空间下的paddle::xxx
用法都会被解析成paddle::distributed::paddle::xxx
更底层的原因
在编译的过程中,需要对
paddle::xxx
用法进行解析,需要先找到符号paddle
的声明或定义,由于paddle
是一个非限定名称(没有前缀::
),因此查找过程的顺序是:先在当前命名空间paddle::distributed
进行查找,如果没找到就在上一层命名空间进行找,直到找完全局命名空间,ref: https://en.cppreference.com/w/cpp/language/unqualified_lookup在上面的例子中,由于在
paddle::distributed
命名空间下使用PD_DEFINE_int32
会定义一个paddle
命名空间,编译器在解析后续的paddle
名称时会先找到paddle::distributed::paddle
,从而所有paddle::xxx
用法都会被解析成paddle::distributed::paddle::xxx
,然后编译器再进一步按照限定名称查找这些用法的声明或者定义,最后没找到报错:Solution