内联这个函数还是不行? [英] Inlining this function or not?

查看:182
本文介绍了内联这个函数还是不行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该实现一个函数比较两个字符串类似 strcmp 但忽略空白字符,所以

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

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

应该给出相同的结果。

这是我的实现:

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);
}

现在的问题是,内联SkipWhitespace函数是否有意义?我想我在某处读到 inline 不应该用于包含循环或开关的函数,但我不记得在哪里和为什么。

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.

推荐答案

历史上,Inline已经表明编译器应该将函数体插入调用站点。然而,这不再是一个有意义的注释。现代编译器将内联函数,不管是否存在 inline 限定。

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.

在现代使用中,inline只有一个函数。它可以用于让链接器忽略多个符号,就像在多个编译单元中定义函数一样。这种技术可以用来打破循环依赖。无需其他用途即可直接使用。

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.

这篇关于内联这个函数还是不行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆