Is there a way that I can create a function that takes an int template parameter, and have that function give a compile time error if the value passed to the function is less than 10?
The following code does not work, but it shows what I want to accomplish:
template <int number1> void reportErrorIfLessThan10() { #if(number1 < 10) #error the number is less than 10 #endif } int maint(int argc, char**argv) { reportErrorIfLessThan10<5>();//report an error! reportErrorIfLessThan10<12>();//ok return 0; }
If you don’t want Boost C++ Libraries magic and want bare bones…
Then use StaticAssert. It’s a #define for me because I have code that needs to run in a lot of environments where C++ doesn’t work right for templates and I need to just back it off to a runtime assert. 🙁
Also, not the best error messages.