4

I'm trying to port some C++ code from Windows to OS X (using Xcode).

The following code:

writePosition %= std::size(bufferL);

is generating an error:

No member named 'size' in namespace 'std'

How do I fix this?

Matthias
  • 4,481
  • 12
  • 45
  • 84
Chris Share
  • 41
  • 1
  • 2

3 Answers3

12

std::size() is available starting from C++17. Try enabling -std=c++17 for your compiler.

Also, double check that the source files contain #include <iterator>, either directly, or indirectly by #include'ing any of the following headers:

  • <array>
  • <deque>
  • <forward_list>
  • <list>
  • <map>
  • <regex>
  • <set>
  • <string>
  • <string_view>
  • <unordered_map>
  • <unordered_set>
  • <vector>
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Dev Null
  • 4,731
  • 1
  • 30
  • 46
2

Taking the info from cppreference i see that std::size accepts two kinds of parameters: containers that have a method called size() (from the stl or user defined) and fixed size arrays.

You should check if bufferL is one of these.

Also you have to include the iterator header file if bufferL is a fixed size array and you haven't included any headers containing containers from the stl.

Tas
  • 7,023
  • 3
  • 36
  • 51
Petok Lorand
  • 955
  • 6
  • 15
  • 2
    While what you say is true, most of it doesn't address the OP's actual question about why `std::size` is unknown to the compiler, regardless of what is being passed to it. The only part of your answer that is relavant to the question is "*you have to include the `iterator` header file*". – Remy Lebeau Jul 26 '18 at 07:50
2

To be able to use std::size you would have to ensure to include #include <iterator>. The other thing you would have to check is the compiler supports C++17. This functionality is only available for compiler which are compliant with C++17 standard.

Change the compiler setting in your IDE to C++17 supported compiler.

Shrikanth N
  • 652
  • 3
  • 17
  • 2
    This doesn't answer the question. If the type of `bufferL` was different, you'd get `no matching function to call`, and not what the OP got. And both links provided aren't helpful in this context. – Bartek Banachewicz Jul 26 '18 at 06:27
  • I was trying to explain what size does and why it may not be working for him. These links are provided as alternate solutions that he could try. Please, correct me if I got it wrong. – Shrikanth N Jul 26 '18 at 06:33
  • 1
    I believe I just did. – Bartek Banachewicz Jul 26 '18 at 06:33
  • 1
    @ShrikanthN explaining what `std::size` does doesn't address the OP's actual question about why `std::size` is unknown to the compiler. Nothing in your answer addresses that question. And using `size_t` has nothing to do with fixing that issue. And using `sizeof()` as a replacement is just plain wrong, it can't be used to get the `size` of containers, it is only possible with fixed arrays, and even then you have to remember to divide the size of the array by the size of the element type to get the proper `size` value. – Remy Lebeau Jul 26 '18 at 07:52
  • @RemyLebeau @ Bartek Banachewicz - thank you for your guidance. I have now modified my answer, hopefully it should be more helpful now. – Shrikanth N Jul 26 '18 at 09:17