19

On a piece of code in a previous question in stackoverflow I saw this, strange to me, declaration with using:

template <std::size_t SIZE> 
class A
{
public:
  ...
  using const_buffer_t = const char(&)[SIZE];
  ...
};

Could someone please address the following questions:

  1. What type it represents?
  2. Where do we need such kind of declarations?
101010
  • 41,839
  • 11
  • 94
  • 168
  • 3
    Time for a C++ book. A C++ book can explain it to you. You'll need an up-to-date one, though. – Lightness Races in Orbit Apr 25 '14 at 14:25
  • @LightnessRacesinOrbit up-to-date books aren't available in many countries in which english isn't the native language, since in order to be published, they have to be translated first (which obviously takes time). I'm quite sure finding an in-depth polish book that covers things introduced in C++11 is quite impossible for example. At least at the local book stores. – Paweł Stawarz Apr 25 '14 at 14:35
  • 2
    @PawełStawarz: Get an English one then... – Lightness Races in Orbit Apr 25 '14 at 14:42
  • 2
    @LightnessRacesinOrbit no real way to do that other than ordering it from a foreign online store, which is quite expensive with all the international shipping fees. There's also the illegal way, but guess there's no need to point out why it`s wrong. And - asking here is always free if its not some obvious information :) – Paweł Stawarz Apr 25 '14 at 14:48
  • 2
    @PawełStawarz Whatever you do, don't get a translated version. I've seen some of the translations of the classics, and the results are often unreadable, and sometimes say just the opposite of what the original said. For the most part, the translators don't know C++, and don't always understand the subtilties involved. – James Kanze Apr 25 '14 at 14:49
  • 2
    FWIW, typically not even programmers understand C++, so that's excusable. :o) – Matteo Italia Apr 25 '14 at 15:12
  • 1
    @PawełStawarz: Amazon ships to most countries relatively cheaply. – Lightness Races in Orbit Apr 25 '14 at 16:15

2 Answers2

18

That's a type alias, a new syntax available since c++11.

What you're actually doing is typedefing the type of an array

const_buffer_t 

will be an array of const char with length = SIZE

Nikos Athanasiou
  • 29,616
  • 15
  • 87
  • 153
  • 4
    To be precise, you should also mention that it is an lvalue reference to array. – Brian Bi Apr 25 '14 at 14:24
  • 1
    @Brian lvalue ness applies to expressions; that's a type (alias). I could define _an lvalue reference to an array_ as being of type `const_buffer_t` as [**well as**](http://ideone.com/vfd5t5) an r value reference. – Nikos Athanasiou Apr 26 '14 at 07:51
  • Actually, I am quite sure that `p2` is also an lvalue reference. If it were an rvalue reference then you couldn't bind it to the lvalue `a`. Anyway, my point is that `using const_buffer_t = const char(&)[SIZE];` does not have the same semantics as `using const_buffer_t = const char[SIZE];`. – Brian Bi Apr 26 '14 at 16:35
14

That using declaration is a new syntax introduced in C++11; it introduces a type alias, specifying that const_buffer_t is now an alias for the type const char(&)[SIZE]. In this respect, this use of using is substantially identical to a typedef (although using type aliases are more flexible).

As for the actual type we are talking about (const char(&)[SIZE]), it's a reference to an array of size SIZE; references to array are rarely used, but can have their use:

  • if in some function you want to enforce receiving a reference to an array of a specific size instead of a generic pointer, you can do that with array references (notice that even if you write int param[5] in a function declaration it's parsed as int *);
  • the same holds for returing references to array (documenting explicitly that you are returning a reference to an array of a specific size);
  • more importantly, if you want to allocate dynamically "true" multidimensional arrays (as opposed to either an array of pointers to monodimensional array or a "flat array" with "manual 2d addressing") you have to use them.

See also the array FAQ, where much of this stuff is explained in detail.

Community
  • 1
  • 1
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • Commenting on _It's a reference to an array_ A [reference](http://www.parashift.com/c++-faq/overview-refs.html) is an alias for an object. This here is an alias for a type. This answer is wrong (the subsequent _with using there being substantially a typedef_ contradicts the first statement and fails to mention alias templates) – Nikos Athanasiou Apr 26 '14 at 10:55
  • 2
    OP asked explicitly what type it represents, and I'm talking about it (the aliased type being the subject of my first sentence), especially since references to arrays are little known, of limited usefulness and have an awkward syntax. The `using` declaration here is mostly an accident, being used here just as a C++11 fancy-pants version of `typedef`. Yes, you can say many other things about the "new" `using`, but since the focus seemed to me on the bizarre array references I just said the essential about how `using` is used in this context. – Matteo Italia Apr 26 '14 at 12:40
  • (Anyhow, for sake of completeness, I expanded a little the explanation about `using`.) – Matteo Italia Apr 26 '14 at 12:47