I understand that inline functions are functions whose bodies are inserted into the place at which they are called. Then why therefore aren’t inline functions affected by scope changes when they are invoked:
#include <iostream>
inline void alert(const std::string &str) { cout << str; }
int main() {
using namespace std;
alert("Hello World"); // cout << "Hello World";
}
This doesn’t work because I get the error cout was not declared in this scope, but if I do std::cout it does. Why doesn’t C++ know that cout is a member of std if the function body of inline functions are inserted into the scope?
The behavior you are describing is a macro. Inline function is a regular function and it is up to the compiler to inline it or not. It behaves exactly as any other function with regards to scoping rules.