template <typename T, std::size_t N>
std::size_t count_of(const T (&)[N])
{
return N;
}
Strange Code
I was looking through one of my friend's projects and I came across this code, I've never seen anything like it and was hoping that someone could explain it to me.
Thanks in Advance.
That's an attempt to get the span of an array. For example,
should work. The declaration of the array must be in scope, so I can't see how useful it would be except as a helper template in metaprogramming.
int x[3]; assert(3 == count_of(x));
should work. The declaration of the array must be in scope, so I can't see how useful it would be except as a helper template in metaprogramming.
Basically, instead of having to pass in the size and type of the array every time for a constant sized array, you let the compiler do it for you.
For example,
Would return five. count_of() requires two template arguments -- when it sees the argument "ints," it goes back to the array and says "oh, it is type int and size 5" -- therefore, passing in 5 as N, which is returned as the size.
At least, that is my best guess as to how it works. I am definitely not a template master.
For example,
int ints[5];std::cout << count_of(ints) << std::endl;
Would return five. count_of() requires two template arguments -- when it sees the argument "ints," it goes back to the array and says "oh, it is type int and size 5" -- therefore, passing in 5 as N, which is returned as the size.
At least, that is my best guess as to how it works. I am definitely not a template master.
Right. The part that is confusing, though is the (&)[N] part. Since the variable that is passed in (which is a reference to an array of N T's) isn't used, they just left it out. I think it's probably clearer to read if the variable name is left in, like this.
I think that the only real benefit this has over just a sizeof(array) / sizeof(type) is that it'll barf at compile time if you try to use it with a dynamically sized array.
Does anyone have an example where this is better than sizeof/sizeof (besides style points for using meta programming)?
template <typename T, std::size_t N>std::size_t count_of(const T (&unused_array_variable)[N]){ return N;}
I think that the only real benefit this has over just a sizeof(array) / sizeof(type) is that it'll barf at compile time if you try to use it with a dynamically sized array.
Does anyone have an example where this is better than sizeof/sizeof (besides style points for using meta programming)?
Thank you all for the help. The part that was confusing me was the second template argument (the size of the array) and the syntax for passing the nameless array by reference.
Once again, thanks.
Once again, thanks.
Quote:Original post by mmelson
Right. The part that is confusing, though is the (&)[N] part. Since the variable that is passed in (which is a reference to an array of N T's) isn't used, they just left it out. I think it's probably clearer to read if the variable name is left in....
If you leave the variable in and you've turned on a reasonable level of compiler warnings, you will get a whackload of "unused variable" warnings. There's a learning curve, but you're better off leaving out the parameter name and learning to parse the byzantine C declaration syntax.
You should always strive to have your code compile warning-free at the highest possible warning level. Really.
Quote:Original post by Bregma
You should always strive to have your code compile warning-free at the highest possible warning level. Really.
Quoted for emphasis.
I totally agree. Though, I would do it using an UNUSED() macro to remove those warnings. I prefer this method because it makes the fact that you are purposefully not using the variable explicit.
I didn't mention it, though, because I thought it was unrelated to the topic, and any discussion on how I would go about avoiding unused variable warnings would have just been a non sequitur. But, since it was brought up, I think it's fair game. =)
I didn't mention it, though, because I thought it was unrelated to the topic, and any discussion on how I would go about avoiding unused variable warnings would have just been a non sequitur. But, since it was brought up, I think it's fair game. =)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement