tags:

views:

120

answers:

2

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

+1  A: 

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 I1, class I2, class T> inline T inner_product(T1 first1, T1 last1, T2 first2, T init)
{
  for (; first != last; ++first1, ++first2) init = init + *first1 * *first2; return init;
}
turdfurguson
+1  A: 

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...

Jerry Coffin