Is there a way to detect inline function ODR violations?

前端 未结 2 1853
一个人的身影
一个人的身影 2021-01-04 01:54

So I have this code in 2 separate translation units:

// a.cpp
#include 
inline int func() { return 5; }
int proxy();
int main() { printf(\"%d\         


        
相关标签:
2条回答
  • 2021-01-04 02:18

    The simplest way to detect such concerns is to copy all the functions into a single compilation unit (create one temporarily if needed). Any C++ compiler will then be able to detect and report duplicate definitions when compiling that file.

    0 讨论(0)
  • 2021-01-04 02:34

    The tools are imperfect.

    I think Gold's check will only notice when the symbols have different types or different sizes, which isn't true here (both functions will compile to the same number of instructions, just using a different immediate value).

    I'm not sure why -Wodr doesn't work here, but I think it only works for types, not functions, i.e. it will detect two conflicting definitions of a class type T but not your func().

    I don't know anything about ASan's ODR checking.

    0 讨论(0)
提交回复
热议问题