-2

I am trying to find the sum of all the elements in an array, and declaring my initial accumulator variable sum as 0 using auto.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    int n;
    auto sum {0};
    cin >> n;
    vector<int> arr(n);
    for(int arr_i = 0;arr_i < n;arr_i++){
        cin >> arr[arr_i];
        sum=sum+arr[arr_i];
     }
     cout<<sum;
     return 0;
}

It is giving me a compilation error. I want to know what is wrong with this.

error: no match for 'operator+' in 'sum + arr.std::vector<_Tp, _Alloc>::operator[] >(((std::vector::size_type)arr_i))'|

I'm using code blocks with gcc compiler and yes C++ 11 is enabled.

skypjack
  • 49,335
  • 19
  • 95
  • 187
  • 3
    In `auto sum{0}`, `sum` is an `std::initializer_list` with one element, `0`. – Tavian Barnes Apr 29 '16 at 21:23
  • 1
    [Not reproduced](http://coliru.stacked-crooked.com/a/4d03e269a9019949). Compile your code with C++11 enabled. – LogicStuff Apr 29 '16 at 21:23
  • @TavianBarnes, no, it is not. – SergeyA Apr 29 '16 at 21:25
  • What compiler are you using? What compiler flags are you using? Please add them to the post. Also add the compiler error to the post. – R Sahu Apr 29 '16 at 21:27
  • 1
    *It is giving me a compilation error*. Well, what error? Can we see it? – skypjack Apr 29 '16 at 21:27
  • and please add it to the question, not as a comment. – Nicolas Holthaus Apr 29 '16 at 21:28
  • @SergeyA This was true until [N3922](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3922.html). I assume his compiler still implements the old rule. – Tavian Barnes Apr 29 '16 at 21:29
  • @TavianBarnes, now it is a correct statement! :) – SergeyA Apr 29 '16 at 21:30
  • @SergeyA Honestly I was surprised that they retconned the C++11 standard for this, I assumed it was too late for that. Or did the compilers just decide to do it in C++11 mode too for convenience? – Tavian Barnes Apr 29 '16 at 21:34
  • @TavianBarnes, I believe it to be C++11 (though I do not have a copy in front of me). CLang actually has an interesting behaviour in versions 3.6 and 3.7 (in c++11 mode) - it errors, but says that behavior will be changed in next version. I find it cute. – SergeyA Apr 29 '16 at 21:38
  • @SergeyA Indeed. They also explain that the C++11 and 14 modes were both changed [per the request of the C++ committee](http://clang.llvm.org/cxx_status.html#n3922). – Tavian Barnes Apr 29 '16 at 21:41
  • What version of gcc are you using? According to [this](http://scottmeyers.blogspot.com/2015/09/thoughts-on-vagaries-of-c-initialization.html), gcc 5.1 implements N3922. If you're using an older version, then TavianBarnes' comment explains the reason for the error – Praetorian Apr 29 '16 at 21:54

1 Answers1

2

In C++11, when you use

auto sum {0};

thensum is of type std::initializer_list<int> and contains one element, 0. This is because the {0} is a braced initialization list.

Using = 0; or (0) should work properly for your code, e.g:

auto sum = 0;
auto sum(0);

EDIT: According to the comments this was NOT what programmers usually expected, hence it is bound to change in C++17 with the N3922 proposal, which is already implemented in newer versions of GCC and Clang, with even for -std=c++11 and -std=c++14 per the request of the C++ committee.

mceo
  • 1,221
  • 11
  • 18
jotik
  • 17,044
  • 13
  • 58
  • 123
  • 3
    It does you no good to repeat after @TavianBarnes - this user is mistaken. – SergeyA Apr 29 '16 at 21:26
  • @SergeyA I'm sorry I don't understand what you're saying? – jotik Apr 29 '16 at 21:36
  • Your answer appeared after comment from Tavian, and is very close to it in wording. However, both your answer and Tavian's initial comment are incorrect. – SergeyA Apr 29 '16 at 21:40
  • 1
    @SergeyA Yeah, I noticed that right after posting my answer, so I changed mine. I didn't plagiarize, chill. :-) You're welcome to post a correct answer. – jotik Apr 29 '16 at 21:44
  • since gcc 5.1.0 `auto sum{0}` is an int, so it appears OP is using old compiler – marcinj Apr 29 '16 at 21:45
  • @MarcinJędrzejewski GCC 5 is too new on most systems. The latest stable GCC 4.9.3 on my Gentoo machine failed with the exact same error the OP later posted. Not everybody is using as bleeding-edge as compiler developers / C++ evangelists do. – jotik Apr 29 '16 at 21:47
  • @jotik with http://melpon.org/wandbox/ you can test all possible gcc versions with a single click. But I agree, at work I am stuck with gcc 4.9 and even VS2005,.... – marcinj Apr 29 '16 at 21:49
  • 2
    @SergeyA How are you so sure this answer is incorrect? Yes, N3922 is a DR that applies to C++11 and later, but the OP could be using an older gcc version that doesn't implement the DR. – Praetorian Apr 29 '16 at 21:50
  • 1
    @MarcinJędrzejewski Took me about 10 clicks to turn un-adblock all the javascript used by that site. Note that many people (including me and probably including the OP) have originally no idea such tools exists, and one shouldn't expect them to. – jotik Apr 29 '16 at 21:52
  • @Praetorian, without additional wording, the answer seems to be talking about current c++11 standard - and is incorrect as such. Be it a an answer with explanation why and how, I would upvote it instead of downvoting. – SergeyA Apr 29 '16 at 21:56
  • @SergeyA Agreed, the answer the could be improved a lot by mentioning the DR and the resulting change in behavior. It's not worthy of an upvote in its current state :) – Praetorian Apr 29 '16 at 22:00