【问题标题】:Inlining std::inner_product内联 std::inner_product
【发布时间】:2009-10-20 17:11:08
【问题描述】:

据称内联 std::inner_product() 不会被 gcc 编译器内联 bug 。

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

谢谢

【问题讨论】:

  • 我是否遗漏了什么,或者第三条消息 (mail-archive.com/gcc-bugs@gcc.gnu.org/msg200159.html) 是否说已修补?
  • 已在 4.1X 版本中修复。我还在 3.4 :( ,因此需要我自己的 std::inner_product 实现。我基本上看到了那个人描述的问题,此时不能接受补丁。

标签: c++ gcc stl inline


【解决方案1】:

您只需要查看您的 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;
}

【讨论】:

    【解决方案2】:

    明显的实现如下所示:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-09
      • 2012-04-18
      • 1970-01-01
      • 2017-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多