Consider the following case
typedef void (*foo)();
template<foo f>
struct bar {
static_assert(f!=nullptr,"f == null!");
};
void baz() {}
inline void bax() { }
bar<baz> ok;
bar<bax> bad; // error: non-constant condition for static assertion
Both baz
and bax
are accepted as template arguments.
It indicates that both are accepted as constants.
However, at static_assert
they appears to be different (at least in gcc 4.9) - bax
is not a constant anymore.
My assumption was that static_assert
and template evaluate constantness identically.
E.g. either error should be
static_assert
should not raise non-constant condition error.Am I wrong?
static_assert is a keyword defined in the <assert. h> header. It is available in the C11 version of C. static_assert is used to ensure that a condition is true when the code is compiled.
@cpplearner Defining any member function inside a class definition makes it implicitly inline . It doens't matter if it's static or not. Instead static member functions means something completely different and is still unrelated to the "inline-ness" of a member function.
The C++ 11 standard introduced a feature named static_assert() which can be used to test a software assertion at the compile time. Syntax: static_assert( constant_expression, string_literal ); Parameters: constant_expression: An integral constant expression that can be converted to a Boolean.
Answer: Static_assert is evaluated at compile time as against the assert () statement that is evaluated at run time. Static_assert has been incorporated in C++ from C++11 onwards. It takes the conditional expression and a message to be displayed as arguments.
When a function is inlined, the pointer to the function does not exist. So we can not compare it with nullptr.
Whether a function is eventually inlined or not, depends on the compiler. inline
keyword does not guarantee that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With