No member named 'begin' in the namespace 'std'

I successfully compiled code on Windows that should be cross-platform. Now, 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 an error occur No member named 'begin' in namespace 'std'?

+4
source share
2 answers

std::beginwas introduced with C ++ 11. See this answer on how to enable C ++ 11 in XCode 4.2 (the exact dialect name has probably changed by now).

Alternatively, if you cannot upgrade to C ++ 11, switch to std::vectorand use v.begin().

+5
source

++ 03. , IDE ++ 11. XCode 4.2 ++ 11 .

std::sort(std::valarray::begin(v), std::valarray::end(v)); - , , - . , valarray begin end Koenig .

std::valarray / . ++ 03 [] .

valarray .. ,

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(); }
}

notstd::begin valarray.

+1

Source: https://habr.com/ru/post/1656810/


All Articles