-2

I'm trying to understand this piece of C++ code from a text book:

template<typename T, int N>
struct Buffer {
  using value_type = T;
  constexpr int size() { return N; }
  T[N];
  // ...
};

It demonstrates stack allocation of the T[N] array, but the array is not named. Later in the text this declaration is made:

Buffer<int,10> buf;

but it's not clear how to access the array buried inside this struct. Does the //... have to include some accessors or is there some implicit facility of the language that can be used to get at the ints[10] that live within buf?

3
  • 2
    What compiler did you try it with? GCC in C++11 mode gives "error: expected unqualified-id before '[' token" Aug 29, 2015 at 23:44
  • I guess that answers it, it must be a typo in the text.
    – James S.
    Aug 30, 2015 at 0:55
  • 4
    I'm voting to close this question as off-topic because it is an implementation question that doesn't meet Stack Overflow's quality standards for migration.
    – durron597
    Sep 3, 2015 at 22:29

1 Answer 1

0

Maybe there some mistake, code does not even compile. So correct code would be like

template<typename T, int N>
struct Buffer {
using value_type = T;
constexpr int size() { return N; }
T b[N];
// ...
};

Edit: I tested it using gnu gcc 4.7.1 compiler in c++11 mode.

1
  • then the array could be accessed via buf.b[]
    – James S.
    Aug 30, 2015 at 0:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.