Ruby templates: How to pass variables into inlined ERB?

For a simple solution, use OpenStruct: require ‘erb’ require ‘ostruct’ namespace = OpenStruct.new(name: ‘Joan’, last: ‘Maragall’) template=”Name: <%= name %> <%= last %>” result = ERB.new(template).result(namespace.instance_eval { binding }) #=> Name: Joan Maragall The code above is simple enough but has (at least) two problems: 1) Since it relies on OpenStruct, an access to a … Read more

static constexpr variable vs function

constexpr functions Functions have an advantage that free variables do not have (until C++14 that is): they can easily be templated without some class boilerplate. That means you can have your pi with a precision depending on a template argument: template<typename T> constexpr T pi(); template<> constexpr float pi() { return 3.14f; } template<> constexpr … Read more

Why does the order of template argument substitution matter?

As stated C++14 explicitly says that the order of template argument substitution is well-defined; more specifically it will be guaranteed to proceed in “lexical order and halt whenever a substitution causes the deduction to fail. Compared to C++11 it will be much easier to write SFINAE-code that consists of one rule depending on another in … Read more

When to use template vs inheritance

The way I see it is that templates and inheritance are literally orthogonal concepts: Inheritance is “vertical” and goes down, from the abstract to the more and more concrete. A shape, a triange, an equilateral triangle. Templates on the other hand are “horizontal” and define parallel instances of code that knowns nothing of each other. … Read more

C++ syntax for explicit specialization of a template function in a template class?

You can’t specialize a member function without explicitly specializing the containing class. What you can do however is forward calls to a member function of a partially specialized type: template<class T, class Tag> struct helper { static void f(T); }; template<class T> struct helper<T, tag1> { static void f(T) {} }; template<class T> struct C … Read more

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