5

I successfully compiled on Windows a code that should be crossplatform. Now when compiling it in Xcode with Mac OS X, I get:

std::valarray<float> v(32);
...
std::sort(begin(v), end(v));            # Use of undeclared identifier 'begin'
std::sort(std::begin(v), std::end(v));  # No member named 'begin' in namespace 'std'
std::sort(std::valarray::begin(v), std::valarray::end(v));  # Idem, error as well

Why does the error No member named 'begin' in namespace 'std' happen?

Basj
  • 41,386
  • 99
  • 383
  • 673
  • 9
    Non-member `begin` is a C++11 feature, try enabling it in XCode. – TartanLlama Oct 05 '16 at 13:16
  • 1
    @Basj: just saw that while testing :) I confirm that adding -std=c++11 to the command line it compiles fine. I'm glad I haven't answered without testing :) – Jean-François Fabre Oct 05 '16 at 13:18
  • 3
    when will std=c++11 will become default? come on it's 2016 and there's a lot of questions only solved by adding that switch! – Jean-François Fabre Oct 05 '16 at 13:19
  • @Jean-FrançoisFabre I assume it is some old XCode verson? I mean, it wouldn't be user-friendly to not include C++11, and this is on a mac, axiomatically it is user-friendly. heh. – Yakk - Adam Nevraumont Oct 05 '16 at 13:23
  • Just for anyone else looking up, here's one more possibility with maps. If your map is a data member and you're trying to access it inside a non-static class method that is qualified as `const`, then accessing mymap[x] will result in compile time error, because, mymap[x] will create an entry for x if it's already not there, so it's modifying the data member in a function that's marked as `const`, so violation. Use mymap.at(x), if you know that x is already present in mymap, using map.find(x) – ppadhy Sep 22 '21 at 06:21

2 Answers2

6

std::begin was introduced with C++11. See this answer for how to enable C++11 in XCode 4.2 (the precise name of the dialect has probably changed by now).

Alternatively, if you can't move to C++11, switch to std::vector and use v.begin().

Community
  • 1
  • 1
2

You could be compiling in C++03 mode. Work out how to get your IDE to compile in C++11 mode. XCode 4.2 turn on C++11 may help.

std::sort(std::valarray::begin(v), std::valarray::end(v)); -- I don't think the standard demands this ever work. I guess if valarray implemented begin and end as statics or Koenig friend operators or somesuch.

std::valarray doesn't come with member begin/end. The only way in C++03 to iterate over it was to use [] or with pointers.

valarray is guaranteed to be contiguous.. So you can write

namespace notstd {
  // addressof taken from http://en.cppreference.com/w/cpp/memory/addressof
  template<class T>
  T* addressof(T& arg) {
    return reinterpret_cast<T*>(
           &const_cast<char&>(
              reinterpret_cast<const volatile char&>(arg)));
  }

  template<class T>
  T* begin( std::valarray<T>& v ) { return addressof(v[0]); }
  template<class T>
  T* end( std::valarray<T>& v ) { return begin(v)+v.size(); }
  template<class T>
  T const* begin( std::valarray<T> const& v ) { return addressof(v[0]); }
  template<class T>
  T const* end( std::valarray<T> const& v ) { return begin(v)+v.size(); }
}

and use notstd::begin on your valarray.

Community
  • 1
  • 1
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524