Empty function macros

You’re absolutely correct, the empty macro doesn’t generate any code. I’ve seen two places where this is useful. The first is to eliminate warnings when a function parameter isn’t used: #define UNUSED(x) int foo(int UNUSED(value)) { return 42; } The second is when you use conditionals to determine if there should be code or not. … Read more

Can I redefine a C++ macro then define it back?

As greyfade pointed out, your ___T___ trick doesn’t work because the preprocessor is a pretty simple creature. An alternative approach is to use pragma directives: // juice includes here #pragma push_macro(“T”) #undef T // include boost headers here #pragma pop_macro(“T”) That should work in MSVC++ and GCC has added support for pop_macro and push_macro for … Read more

WRITE_ONCE in linux kernel lists

The first definition you refer to is part of the kernel lock validator, aka “lockdep”. WRITE_ONCE (and others) don’t need special treatment, but the reason why is the subject of another question. The relevant definition would be here, and a very terse comment states their purpose to be: Prevent the compiler from merging or refetching … Read more

Error: macro names must be identifiers using #ifdef 0

The #ifdef directive is used to check if a preprocessor symbol is defined. The standard (C11 6.4.2 Identifiers) mandates that identifiers must not start with a digit: identifier: identifier-nondigit identifier identifier-nondigit identifier digit identifier-nondigit: nondigit universal-character-name other implementation-defined characters> nondigit: one of _ a b c d e f g h i j k l … Read more

Can I convert a string to enum without macros in Rust?

You should implement std::str::FromStr trait. use std::str::FromStr; #[derive(Debug, PartialEq)] enum Foo { Bar, Baz, Bat, Quux, } impl FromStr for Foo { type Err = (); fn from_str(input: &str) -> Result<Foo, Self::Err> { match input { “Bar” => Ok(Foo::Bar), “Baz” => Ok(Foo::Baz), “Bat” => Ok(Foo::Bat), “Quux” => Ok(Foo::Quux), _ => Err(()), } } } fn … Read more

Is there a way to get function name inside a C++ function?

C99 has __func__, but for C++ this will be compiler specific. On the plus side, some of the compiler-specific versions provide additional type information, which is particularly nice when you’re tracing inside a templatized function/class. MSVC: __FUNCTION__, __FUNCDNAME__, __FUNCSIG__ GCC: __func__, __FUNCTION__, __PRETTY_FUNCTION__ Boost library has defined macro BOOST_CURRENT_FUNCTION for most C++ compilers in header … Read more

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