Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine C++ inline functions?

I have the following piece of code.

#include <iostream>

using namespace std;

inline void inlinefunc() { cout << "hello" << endl; }
void func() { cout << "hello" << endl; }

bool isInlineFunc(void (*f)()) { return (f == inlinefunc); }

void print(void (*f)()) {
    cout << (isInlineFunc(f)?"inline - ":"normal -");
    f();
}

int main() {
    void (*f)();

    f = inlinefunc;
    print(f);

    f = func;
    print(f);
}

How do I generically write isInlineFunc?

like image 240
Alex Yeung Avatar asked Dec 17 '25 10:12

Alex Yeung


2 Answers

What do you mean generically? Do you want to have a function that tells you if another function is declared inline? That can't be done.

Also note that by taking the address of an inline function, the implementation is forced to actually have an out of line implementation of the function.

like image 137
K-ballo Avatar answered Dec 20 '25 00:12

K-ballo


You don't.
The compiler is basically allowed to do whatever it wants with regards to inlining functions. A function can be inlined in one place and not inlined in another place.

If you're thinking about this, you're probably prematurely optimizing something.

like image 25
shoosh Avatar answered Dec 20 '25 00:12

shoosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!