|
Prev: I got a strange error message when using fstream!
Next: Why is the return value of array<T, 0>::data() unspecified?
From: Marcin.Barczynski on 31 Mar 2008 01:04 On Mar 31, 1:43 am, Jerzie.Klench...(a)gmail.com wrote: > Hello all, > > I have the following piece of code: > > template<int i, int j, int k> > void foo() > { > // rule: i >= j > // static_assert(i >= j) > // static_assert((i - j) >= 2 * k) > > } > > int main() > { > foo<1,2,3>(); > return 0; > > } > > I was wondering how one would go about creating an > assert that runs at compile time which would give > an error message based on the assertion? > > I've seen static asserts for types but haven't seen > any for value templates - any ideas? The easiest way is to declare a template and define only a specialization: template <bool> struct static_assert; template<> struct static_assert<true> { }; Marcin. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |