Template Metaprogramming – Difference Between Using Enum Hack and Static Const

Enums aren’t lvals, static member values are and if passed by reference the template will be instanciated: void f(const int&); f(TMPFib<1>::value); If you want to do pure compile time calculations etc. this is an undesired side-effect. The main historic difference is that enums also work for compilers where in-class-initialization of member values is not supported, … Read more

String-interning at compiletime for profiling

Identical literal strings are not guaranty to be identical, but you can build type from it which can compare identical (without comparing string), something like: // Sequence of char template <char…Cs> struct char_sequence { template <char C> using push_back = char_sequence<Cs…, C>; }; // Remove all chars from char_sequence from ‘\0’ template <typename, char…> struct … Read more

Scala3: Crafting Types Through Metaprogramming?

Just in case, here is what I meant by hiding ViewOf inside a type class (type classes is an alternative to match types). Sadly, in Scala 3 this is wordy. (version 1) import scala.annotation.experimental import scala.quoted.{Expr, Quotes, Type, quotes} // Library part trait View extends Selectable { def applyDynamic(key: String)(args: Any*): Any = { println(s”$key … Read more

How to find out the arity of a method in Python

Module inspect from Python’s standard library is your friend — see the online docs! inspect.getargspec(func) returns a tuple with four items, args, varargs, varkw, defaults: len(args) is the “primary arity”, but arity can be anything from that to infinity if you have varargs and/or varkw not None, and some arguments may be omitted (and defaulted) … Read more

How can I dynamically create class methods for a class in python [duplicate]

You can dynamically add a classmethod to a class by simple assignment to the class object or by setattr on the class object. Here I’m using the python convention that classes start with capital letters to reduce confusion: # define a class object (your class may be more complicated than this…) class A(object): pass # … Read more

What does send() do in Ruby?

send sends a message to an object instance and its ancestors in class hierarchy until some method reacts (because its name matches the first argument). Practically speaking, those lines are equivalent: 1.send ‘+’, 2 1.+(2) 1 + 2 Note that send bypasses visibility checks, so that you can call private methods, too (useful for unit … Read more