Enumerate over an enum in C++

To add to @StackedCrooked answer, you can overload operator++, operator– and operator* and have iterator like functionality. enum Color { Color_Begin, Color_Red = Color_Begin, Color_Orange, Color_Yellow, Color_Green, Color_Blue, Color_Indigo, Color_Violet, Color_End }; namespace std { template<> struct iterator_traits<Color> { typedef Color value_type; typedef int difference_type; typedef Color *pointer; typedef Color &reference; typedef std::bidirectional_iterator_tag iterator_category; }; … Read more

Safe way to extract property names

In TS 2.1 the keyof keyword was introduced which made this possible: function propertyOf<TObj>(name: keyof TObj) { return name; } or function propertiesOf<TObj>(_obj: (TObj | undefined) = undefined) { return function result<T extends keyof TObj>(name: T) { return name; } } or using Proxy export function proxiedPropertiesOf<TObj>(obj?: TObj) { return new Proxy({}, { get: (_, … Read more

Why is partial specialization of a nested class template allowed, while complete isn’t?

My guess as to why this happens: complete specializations are no longer “template classes/functions”, they are are “real” classes/methods, and get to have real (linker-visible) symbols. But for a completely-specialized template inside a partially-specialized one, this would not be true. Probably this decision was taken just to simplify the life of compiler-writers (and make life … Read more

How do I get constants defined by Ruby’s Module class via reflection?

Good question! Your confusion is due to the fact that the class method Module.constants hides the instance method Module#constants for Module. In Ruby 1.9, this has been addressed by adding an optional parameter: # No argument: same class method as in 1.8: Module.constants # ==> All constants # One argument: uses the instance method: Module.constants(true) … Read more

Is constexpr-based computation Turing complete?

tl;dr: constexpr in C++11 was not Turing-complete, due to a bug in the specification of the language, but that bug has been addressed in later drafts of the standard, and clang already implements the fix. constexpr, as specified in the ISO C++11 international standard, is not Turing-complete. Sketch proof: Every constexpr function f‘s result (or … Read more

Getting template metaprogramming compile-time constants at runtime

template <unsigned long N> struct Fibonacci { enum { value = Fibonacci<N-1>::value + Fibonacci<N-2>::value }; static void add_values(vector<unsigned long>& v) { Fibonacci<N-1>::add_values(v); v.push_back(value); } }; template <> struct Fibonacci<0> { enum { value = 0 }; static void add_values(vector<unsigned long>& v) { v.push_back(value); } }; template <> struct Fibonacci<1> { enum { value = 1 … Read more

How to call a method dynamically in Elixir, by specifying both module and method name?

You can use apply/3 which is just a wrapper around :erlang.apply/3. It simply invokes the given function from the module with an array of arguments. Since you are passing arguments as the module and function names you can use variables. apply(:lists, :nth, [1, [1,2,3]]) apply(module_name, method_name, [1, array]) If you want to understand more about … Read more