Hello, right now am making a clone of hangman, and is stuck in one of my member functions :
This class member sets the word to be guessed by the user, i want this function
to static_assert if it finds a non - alphabetic or non lowercase character
but i get an error regarding the assert part :
error: non-constant condition for static assertion
error: the value of 'wordIsValid' is not usable in a constant expression
note: 'int wordIsValid' is not const
1 2 3 4 5 6 7 8 9 10 11 12 13
void word_handle::setWord ( constchar* word )
{
int wordIsValid = 1;
// Check if word is valid
for ( std::size_t i = 0; i < strlen( word ); ++i ) {
if ( ! isalpha( word[i] ) && islower( word[i] ) ) {
wordIsValid = 0;
break;
}
}
static_assert ( wordIsValid == 1, "Word can only contain alphabetic characters and must be lowercase" );
wordToGuess_ = word;
}
Please help me, i don't really know if static_assert will work here ?
static_assert is a compile-time only operation, it cannot examine the value of your wordIsValid variable, because it doesn't yet exist when the program is compiled.
If you try to compile this, you should get a diagnostic such as
test.cc:11:21: error: static_assert expression is not an integral constant expression
static_assert ( wordIsValid == 1, "Word can only contain alphabetic...
^~~~~~~~~~~~~~~~