Sum of const vector

Issue #392 resolved
Pedro Alves created an issue


I’m working in a library with functions with this signature:

float calculate_sum(const vector<float> &v)

The goal is to calculate the sum of all the elements of a vector. Obviously this is a trivial example. But most of the work will be based in this type of signature (const vector). Reading the wiki and a few of the issues I got this far:

#include "blaze/Math.h"
using blaze::CustomVector;
using blaze::unaligned;
using blaze::unpadded;
using blaze::columnVector;
using blaze::sum;

#include <iostream>
using std::cout;
using std::endl;

#include <vector>
using std::vector;

using bVector = CustomVector<float const,  unaligned, unpadded, columnVector>;

float calc_sum (const vector<float> &v) noexcept {
    bVector blaze_v(, v.size());
    // return 0.0f;
    return sum(blaze_v);

int main() {
    vector<float> v{1.0f, 2.0f, 3.0f};
    std::cout << calc_sum(v) << std::endl;
    return 0;

It works if I take out the const… Unfortunately, that is not a possibility…

Is blaze a good fit for this? What am I doing wrong?

Thanks in advance!

Comments (6)

  1. Klaus Iglberger

    Hi Pedro!

    Thanks a lot for creating this issue. You aren’t doing anything wrong, but unfortunately we do, since we didn’t encounter or consider this case before. Within the sum() function we use the element type of the given vector (in this case the CustomVector<float const,…>), which currently evaluates to float const instead of float. We’ll fix this problem as quickly as possible. Thanks again,

    Best regards,


  2. Pedro Alves reporter

    Thank you for the very quick reply.

    I don’t know enough about blaze (or even C++) to tell is this is valuable information or not. But other functions like min/mean/max also have this problem.

    I don’t know if this is a fix to all “reduce type“ functions or if you will have to fix every function one by one (assuming you think this is a valid use-case).

    Furthermore, I assumed I was the one doing wrong because, after reading you extensive documentation, your clearly say:

    Type : specifies the type of the vector elements. CustomVector can be used with any non-cv-qualified, non-reference, non-pointer element type.

    (from Types)

    I assume cv means const or volatile.

    Reading other issues, I think you are ok with const, right? So either I’m not reading the wiki correctly (strong possibility) or something is not right.

  3. Klaus Iglberger

    Hi Pedro!

    Thanks a lot for pointing out the other functions. Luckily, all of these are based on the implementation of the reduce() function. We have fixed this with the last push, so you should not experience problems anymore.

    You are also right about the second issue, the tutorial and the wiki were not correct. We have fixed these as well. Thanks for helping to make Blaze a better library!

    Best regards,


  4. Klaus Iglberger

    Commit daf5b7c fixes the reduce() functions for CustomVector and CustomMatrix with const-qualified element types and commit 82a66fd fixes the documentation of CustomVector and CustomMatrix in the tutorial. The fixes are immediately available via cloning the Blaze repository and will be officially released in Blaze 3.9.

  5. Log in to comment