Sen static_assert
declaration ile yapabilirsiniz:
template<int N> void tryHarder() {
static_assert(N >= 0 && N <= 10, "N out of bounds!");
for(int i = 0; i < N; i++) {
tryOnce();
}
}
Bu özellik C++ 11 beri sadece DAİREMİZ. C++ 03 ile sıkışmışsanız, Boost's static assert macro'a bakın.
bu tüm fikri güzel hata mesajlarıdır. O umursamıyorum veya hatta boost affor, bir şeyler yapabileceğini aşağıda belirtildiği şekilde:
template<bool B>
struct assert_impl {
static const int value = 1;
};
template<>
struct assert_impl<false> {
static const int value = -1;
};
template<bool B>
struct assert {
// this will attempt to declare an array of negative
// size if template parameter evaluates to false
static char arr[assert_impl<B>::value];
};
template<int N>
void tryHarder()
{
assert< N <= 10 >();
}
int main()
{
tryHarder<5>(); // fine
tryHarder<15>(); // error, size of array is negative
}
(http://en.cppreference.com/w/cpp/language/static_assert) – juanchopanza
@juanchopanza [static_assert] göz at: O cevaptır. – Nawaz
Harika görünüyor! Ama C++ 11 öncesi bir şey var mı? – MciprianM