How can you compare two character strings statically at compile time

Starting with C++17 std::string_view is available. It supports constexpr comparisson: #include <string_view> constexpr bool strings_equal(char const * a, char const * b) { return std::string_view(a)==b; } int main() { static_assert(strings_equal(“abc”, “abc” ), “strings are equal”); static_assert(!strings_equal(“abc”, “abcd”), “strings are not equal”); return 0; } Demo

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

How can I define a string literal on the GCC command line?

Two options. First, escape the quotation marks so the shell doesn’t eat them: gcc -Dname=\”Mary\” Or, if you really want -Dname=Mary, you can stringize it, though it’s a bit hacky. #include <stdio.h> #define STRINGIZE(x) #x #define STRINGIZE_VALUE_OF(x) STRINGIZE(x) int main(int argc, char *argv[]) { printf(“%s”, STRINGIZE_VALUE_OF(name)); } Note that STRINGIZE_VALUE_OF will happily evaluate down to … Read more

How to compile and execute scala code at run-time in Scala3?

Scala 2 version of this answer is here: How can I run generated code during script runtime? In Scala 3: For example you can use Li Haoyi‘s Ammonite ammonite.Main(verboseOutput = false).runCode(“””println(“Hello, World!”)”””) // Hello, World! build.sbt scalaVersion := “3.1.3” libraryDependencies += “com.lihaoyi” % “ammonite” % “2.5.4-22-4a9e6989” cross CrossVersion.full excludeDependencies ++= Seq( ExclusionRule(“com.lihaoyi”, “sourcecode_2.13”), ExclusionRule(“com.lihaoyi”, “fansi_2.13”), … Read more

Macros to create strings in C

In C, string literals are concatenated automatically. For example, const char * s1 = “foo” “bar”; const char * s2 = “foobar”; s1 and s2 are the same string. So, for your problem, the answer (without token pasting) is #ifdef __TESTING #define IV_DOMAIN “example.org” #elif __LIVE_TESTING #define IV_DOMAIN “test.example.com” #else #define IV_DOMAIN “example.com” #endif #define … Read more

What is ‘:-!!’ in C?

This is, in effect, a way to check whether the expression e can be evaluated to be 0, and if not, to fail the build. The macro is somewhat misnamed; it should be something more like BUILD_BUG_OR_ZERO, rather than …ON_ZERO. (There have been occasional discussions about whether this is a confusing name.) You should read … Read more

When should I quote CMake variable references?

Two principles of CMake you have to keep in mind: CMake is a script language and arguments are evaluated after the variables are expanded CMake differentiates between normal strings and list variables (strings with semicolon delimiters) Examples set(_my_text “A B C”) with message(“${_my_text}”) would give A B C set(_my_list A B C) with message(“${_my_list}”) would … Read more

How to view C preprocessor output?

gcc -E file.c or g++ -E file.cpp will do this for you. The -E switch forces the compiler to stop after the preprocessing phase, spitting all it’s got at the moment to standard output. Note: Surely you must have some #include directives. The included files get preprocessed, too, so you might get lots of output. … Read more