Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does (&) -- ampersand in parentheses -- mean in this code?

Tags:

In this answer, the following code is given (C++11):

template <typename T, size_t N>
constexpr size_t size_of(T (&)[N]) {
    return N;
}

What does the (&) mean in this context? This sort of thing is exceptionally difficult to search for.

like image 384
Phonon Avatar asked Jun 13 '15 17:06

Phonon


People also ask

What the Fox say Meaning?

Speaking of the meaning of the song, Vegard characterizes it as coming from "a genuine wonder of what the fox says, because we didn't know". Although interpreted by some commentators as a reference to the furry fandom, the brothers have stated they did not know about its existence when producing "The Fox".

What does the fox say for real?

One of the most common fox vocalizations is a raspy bark. Scientists believe foxes use this barking sound to identify themselves and communicate with other foxes. Another eerie fox vocalization is a type of high-pitched howl that's almost like a scream.

What is this song Google?

Ask Google Assistant to name a song On your phone, touch and hold the Home button or say "Hey Google." Ask "What's this song?" Play a song or hum, whistle, or sing the melody of a song. Hum, whistle, or sing: Google Assistant will identify potential matches for the song.


2 Answers

It's shorthand for something like this:

template <typename T, size_t N>
constexpr size_t size_of(T (&anonymous_variable)[N]) {
    return N;
}

In the function, you don't actually need the name of the variable, just the template deduction on N - so we can simply choose to omit it. The parentheses are syntactically necessary in order to pass the array in by reference - you can't pass in an array by value.

like image 156
Barry Avatar answered Sep 20 '22 13:09

Barry


This is passing an array as a parameter by reference:

template <typename T, size_t N>
constexpr size_t size_of(T (&)[N]) {
    return N;
}

As you cannot pass an array like this:

template <typename T, size_t N>
constexpr size_t size_of(T[N]) {
    return N;
}

If the array was given a name, it would look like:

template <typename T, size_t N>
constexpr size_t size_of(T (&arrayname)[N]) {
    return N;
}
like image 22
user3476093 Avatar answered Sep 20 '22 13:09

user3476093