What makes a better constant in C, a macro or an enum?

In terms of readability, enumerations make better constants than macros, because related values are grouped together. In addition, enum defines a new type, so the readers of your program would have easier time figuring out what can be passed to the corresponding parameter. Compare #define UNKNOWN 0 #define SUNDAY 1 #define MONDAY 2 #define TUESDAY … Read more

Equivalent of __func__ or __FUNCTION__ in Rust?

You can hack one together with std::any::type_name. macro_rules! function { () => {{ fn f() {} fn type_name_of<T>(_: T) -> &’static str { std::any::type_name::<T>() } let name = type_name_of(f); name.strip_suffix(“::f”).unwrap() }} } Note that this gives a full pathname, so my::path::my_func instead of just my_func. A demo is available.

So many parentheses in gcc standard headers

If you wrote a = 7 INTMAX_MIN; you would expect to get a syntax error, because on the face of it that would be an illegal expression. And it will, because it expands to a = 7 (-9223372036854775807LL); which does indeed give you a syntax error. But without the parentheses it would expand to: a … Read more

C/C++: How to use the do-while(0); construct without compiler warnings like C4127?

Summary: This warning (C4127) in this particular case is a subtle compiler bug. Feel free to disable it. In depth: It was meant to catch situations when logical expression evaluates to a constant in non-obvious situations (such as, if(a==a && a!=a), and somehow, it turned while(true) and other useful constructs into invalid. Microsoft recommends using … Read more

Escaping a # symbol in a #define macro?

It is possible to insert a hash token into the preprocessed token stream. You can do it as follows: #define MACRO(hash, name) hash include name MACRO(#,”hello”) —expands to: # include “hello” However, the standard explicitly rules out any further analysis of such line for the existence of preprocessing directives [cpp.rescan]: The resulting completely macro-replaced preprocessing … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)