No member named 'begin' in namespace 'std'

后端 未结 2 1608
生来不讨喜
生来不讨喜 2021-01-20 01:06

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 v(32)         


        
2条回答
  •  深忆病人
    2021-01-20 01:19

    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
      T* addressof(T& arg) {
        return reinterpret_cast(
               &const_cast(
                  reinterpret_cast(arg)));
      }
    
      template
      T* begin( std::valarray& v ) { return addressof(v[0]); }
      template
      T* end( std::valarray& v ) { return begin(v)+v.size(); }
      template
      T const* begin( std::valarray const& v ) { return addressof(v[0]); }
      template
      T const* end( std::valarray const& v ) { return begin(v)+v.size(); }
    }
    

    and use notstd::begin on your valarray.

提交回复
热议问题