Non-inline function from another source file takes precedence over inline function

54 views Asked by At

I was experimenting with inline functions in different translation units and stumbled upon this interesting example.

// A.cpp
#include <iostream>

void f() {
    std::cout << "Bye" << std::endl;
}
// main.cpp
#include <iostream>

inline void f() {
    std::cout << "Hello" << std::endl;
}

int main() {
    f();
    return 0;
}

When I compile and link the 2 files with g++ main.cpp A.cpp and run the executable, "Bye" is printed to console. Why is it "Bye" that gets printed and not "Hello", and how why does this not violate One-Definition Rule?

Thanks in advance!

0

There are 0 answers