Re:A nice, little known C feature: Static array indices in p...
by pyrtsa on Monday February 18, 2013 @12:48PM
(#5239484)
And, by extending your second example, we can enforce the minimum array size with std::enable_if (C++11; boost::enable_if_c with C++03):
template <size_t N>
typename enable_if<(N >= 10), void>::type
foo(int (&bar)[N]) { ... }
EDIT: Changed condition from N >= 0 into a more meaningful one. X-)
Re:A nice, little known C feature: Static array indices in p...
by huhtenberg on Monday February 18, 2013 @01:07PM
(#5239583)
... and just as I was going to make a smart-ass joke about Boost probably having another contrived contraption just for that - there it is, already sneaked into the standard too.
Re:A nice, little known C feature: Static array indices in p...
by pbsd on Monday February 18, 2013 @02:06PM
(#5239956)
The more readable C++11 version would use static_assert:
template <size_t N>
foo(int (&bar)[N])
{
static_assert(N >= 10, "Array needs >= 10 elements!");
...
}
Re:A nice, little known C feature: Static array indices in p...
by pyrtsa on Monday February 18, 2013 @02:22PM
(#5240067)
Correct. The key difference is that the enable_if version can be overloaded with mutually exclusive requirements:
template <size_t N>
typename enable_if<(1 < N && N < 10), void>::type
foo(int (&bar)[N])
{
// called when 1 < N && N < 10
}
template <size_t N>
typename enable_if<(N >= 10), void>::type
foo(int (&bar)[N])
{
// called when N >= 10
}
OTOH, the key advantage in static_assert is the meaningful error message.
Re:A nice, little known C feature: Static array indices in p...
by abcd_f on Monday February 18, 2013 @03:01PM
(#5240275)
This will generate separate function bodies for different values of N.
Re:A nice, little known C feature: Static array indices in p...
by zokier on Monday February 18, 2013 @05:01PM
(#5241027)
I don't think that is more readable. With enable_if the precondition is clearly visible at the function header, while the static_assert is inside the body and thus more easily accidentally ignored.
Re:A nice, little known C feature: Static array indices in p...
by obiterdictum on Monday February 18, 2013 @05:45PM
(#5241283)
On the other hand, static_assert will have a more helpful compile error for users of the code. They will clearly see assertion condition that failed, rather than missing candidate error due to substitution failure (or worse, a different overload silently considered instead).
Re:A nice, little known C feature: Static array indices in p...
by chc on Monday February 18, 2013 @01:03PM
(#5239568)
This works with pointers in both C and C++. int (*foo)[10] is a pointer to an array of exactly 10 elements. The novel thing being pointed out in the OP is the "10 or more" aspect.
Re:A nice, little known C feature: Static array indices in p...
by cliffbean on Monday February 18, 2013 @07:01PM
(#5241684)
That's true, until you forget and type foo[i] instead of (* foo)[i]. Of course, compilers will usually catch that mistake at compile time...
There'a an interesting analogy with structs here. K&R invented the -> operator specifically to obviate (* ptr).member. It's too bad that arrays took a different route through C's history and didn't manage to end up in a place where a similar convenience would make sense.
You can also get the size at compile time:
I've used the above for something like this: