static_assert help

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 ( const char* 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 ?
Last edited on
mov my_question, top ; Bump
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...
                    ^~~~~~~~~~~~~~~~
Last edited on
For a run-time assertion, use assert() in <cassert> http://en.cppreference.com/w/cpp/error/assert
Last edited on
oh, i see, tnx for the help guys, i guess i'll try assert ()
Topic archived. No new replies allowed.