Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inlining this function or not?

I'm supposed to implement a function which compares two strings simliar so strcmp but ignoring whitespace characters, so

strcmpignorews("abc   ", " a b c")

should give the same result.

Here's my implementation:

namespace {
    void SkipWhitespace(const char *&s) {
        for (; std::isspace(*s, std::locale::classic); ++s);
    }
}

int strcmpignorews(const char *s1, const char *s2) {
    for (; *s1 != '\0' && *s2 != '\0'; ++s1, ++s2) {
        SkipWhitespace(s1);
        SkipWhitespace(s2);

        if (*s1 != *s2) {
            break;
        }
    }

    return (*s1 < *s2) ? -1 : ((*s1 == *s2) ? 0 : 1);
}

Now, the question is, would it make sense to inline the SkipWhitespace function? I think I've read somewhere that inline should not be used for functions which contain loops or switches but I can't remember where and why.

like image 619
helpermethod Avatar asked Nov 29 '22 04:11

helpermethod


1 Answers

Historically, Inline has been an indication to the compiler that it should insert the function body into the call site. However, that is no longer a meaningful annotation. Modern compilers will inline a function or not regardless of the presence or absence of inline qualification.

To emphasize, whether compiler will perform inline optimization is completely out of your hands.

In modern use, inline has only one function. It can be used to get the linker to ignore multiple symbols, as when a function is defined in multiple compilation units. This technique can be used to break circular dependencies. Use inline for no other purpose.

like image 100
SingleNegationElimination Avatar answered Dec 05 '22 10:12

SingleNegationElimination