内联 std::inner_product

发布于 2024-08-08 06:39:14 字数 238 浏览 3 评论 0原文

据称内联 std::inner_product() 不会被 gcc 编译器内联 <海湾合作委员会4.1 编译器,根据以下 bug

因此我想实现我自己的inner_product版本。是 有现有的实施方案吗?

谢谢

Allegedly inlining std::inner_product() does NOT get inlined with gcc compiler < gcc 4.1
compilers, per the following bug .

Hence I would like to implement my own version of inner_product. Are
there existing implementation available?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灰色世界里的红玫瑰 2024-08-15 06:39:14

您只需查看 C++ 头文件,找到定义,然后使用“inline”关键字(可能在您的命名空间中)重新定义它。例如,查看我的标题:

template <class T1, class T2, class T> inline T inner_product(T1 first1, T1 last1, T2 first2, T init)
{
  for (; first1 != last1; ++first1, ++first2) init = init + *first1 * *first2; return init;
}

You just need to look in your C++ header files, find the definition, and redefine it with the "inline" keyword (possibly in your namespace). For example, looking at my headers:

template <class T1, class T2, class T> inline T inner_product(T1 first1, T1 last1, T2 first2, T init)
{
  for (; first1 != last1; ++first1, ++first2) init = init + *first1 * *first2; return init;
}
梦归所梦 2024-08-15 06:39:14

明显的实现看起来像这样:

// warning: untested code:
template <class I1, class I2, class T>
T inline inner_product(I1 s1, I1 e1, I2 s2, T i) {
    while (s1!=e1) {
        i = i + ((*(s1)) * (*(s2)));
        ++(s1);
        ++(s2);
    }
    return i;
}

template <class I1, class I2, class T, class B1, class B2>
T inline inner_product(I1 s1, I1 e1, I2 s2, T i, B1 b1, B2 b2) {
    while (s1!=e1) {
        i=b1(i, b2(*(s1), *(s2)));
        ++(s1);
        ++(s2);
    }
    return i;
}

使用如此短的标识符可能是有问题的,但是对于像这样存在于标头中的代码,因此它被编译了无数次,短标识符可以节省解析时间......

The obvious implementations would look something like this:

// warning: untested code:
template <class I1, class I2, class T>
T inline inner_product(I1 s1, I1 e1, I2 s2, T i) {
    while (s1!=e1) {
        i = i + ((*(s1)) * (*(s2)));
        ++(s1);
        ++(s2);
    }
    return i;
}

template <class I1, class I2, class T, class B1, class B2>
T inline inner_product(I1 s1, I1 e1, I2 s2, T i, B1 b1, B2 b2) {
    while (s1!=e1) {
        i=b1(i, b2(*(s1), *(s2)));
        ++(s1);
        ++(s2);
    }
    return i;
}

Using such short identifiers is probably questionable, but for code like this that lives in a header so its compiled a gazillion times, short identifiers save parsing time...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文