metaprogramming

Is intptr_t a signed counterpart of uintptr_t (and vice versa)?

梦想的初衷 提交于 2019-12-21 07:58:09
问题 I'm developing some tests for the add_signed MPL class that converts the type to its signed counterpart. It is defined as follows: template<class T> struct add_signed { typedef T type; }; template<> struct add_signed<std::uint8_t> { typedef std::int8_t type; }; template<> struct add_signed<std::uint16_t> { typedef std::int16_t type; }; template<> struct add_signed<std::uint32_t> { typedef std::int32_t type; }; template<> struct add_signed<std::uint64_t> { typedef std::int64_t type; }; While

Statically Typed Metaprogramming?

霸气de小男生 提交于 2019-12-21 07:09:04
问题 I've been thinking about what I would miss in porting some Python code to a statically typed language such as F# or Scala; the libraries can be substituted, the conciseness is comparable, but I have lots of python code which is as follows: @specialclass class Thing(object): @specialFunc def method1(arg1, arg2): ... @specialFunc def method2(arg3, arg4, arg5): ... Where the decorators do a huge amount: replacing the methods with callable objects with state, augmenting the class with additional

C++ metaprogramming with templates versus inlining

两盒软妹~` 提交于 2019-12-21 05:03:15
问题 Is it worth to write code like the following to copy array elements: #include <iostream> using namespace std; template<int START, int N> struct Repeat { static void copy (int * x, int * y) { x[START+N-1] = y[START+N-1]; Repeat<START, N-1>::copy(x,y); } }; template<int START> struct Repeat<START, 0> { static void copy (int * x, int * y) { x[START] = y[START]; } }; int main () { int a[10]; int b[10]; // initialize for (int i=0; i<=9; i++) { b[i] = 113 + i; a[i] = 0; } // do the copy (starting

How would one write a “meta if else if..” in C++?

假如想象 提交于 2019-12-21 04:54:15
问题 I am just learning basics of metaprogramming in C++, and I thought it would be nice to see how the following question would be solved by others. Also, it would be very nice to see a solution using Boost metaprogramming libraries because I consider them as the dark corner for me. So the question is, could this be rewritten more elegantly? Assume that we have the following struct: template <std::size_t size> struct type_factory { typedef typename type_factory_impl<size>::type type; }; This

Nils and method chaining

时间秒杀一切 提交于 2019-12-21 04:06:54
问题 I'm just breaking into the ruby world and I could use a helping hand. Suppose b is nil . I'd like the following code to return nil instead of a "NoMethodError: undefined method" a.b.c("d").e The first thing I tried was to overload NilClass's missing_method to simply return a nil. This is the behaviour I want except I don't want to be this intrusive. I'd love it if I could do something like this SafeNils.a.b.c("d").e So it's like a clean way to locally overload the NilClass's behaviour. I'd

PHP: Wrap all functions of a class in a subclass

◇◆丶佛笑我妖孽 提交于 2019-12-20 19:39:30
问题 Working with a PHP library class, and I'd like to wrap all of its public functions in a subclass... Something along the lines of: class BaseClass { function do_something() { some; stuff; } function do_something_else() { other; stuff; } /* * 20-or-so other functions here! */ } class SubClass extends BaseClass { function magicalOverrideEveryone() { stuff-to-do-before; // i.e. Display header call_original_function(); // i.e. Display otherwise-undecorated content stuff-to-do-after; // i.e.

Easiest way to get the N-th argument of a variadic templated class?

我的未来我决定 提交于 2019-12-20 18:42:17
问题 I wonder what is the easiest and more common way to get the N-th parameter of a variadic templated class at compile-time (The returned value has to be as a static const for the compiler in order to do some optimizations). Here is the form of my templated class : template<unsigned int... T> MyClass { // Compile-time function to get the N-th value of the variadic template ? }; Thank you very much. EDIT : As MyClass will contain more than 200 functions, I can't specialize it. But I can

Easiest way to get the N-th argument of a variadic templated class?

与世无争的帅哥 提交于 2019-12-20 18:40:12
问题 I wonder what is the easiest and more common way to get the N-th parameter of a variadic templated class at compile-time (The returned value has to be as a static const for the compiler in order to do some optimizations). Here is the form of my templated class : template<unsigned int... T> MyClass { // Compile-time function to get the N-th value of the variadic template ? }; Thank you very much. EDIT : As MyClass will contain more than 200 functions, I can't specialize it. But I can

How to detect the end of a method chain in Ruby

梦想的初衷 提交于 2019-12-20 14:12:44
问题 I have a Flickr interface that I wrote a while ago and part of it bothers me and I'd like to make it nicer. The way it works is I use method missing to construct the url parameters for the flickr call from the methods called on the flickr object, eg. @flickr.groups.pools.getPhotos(:user_id => "12656878@N06", :group_id => "99404851@N00") These 'method calls' construct an api call that looks like this http://api.flickr.com/services/rest/?method=groups.pools.getPhotos&user_id=1848466274& group

Equivalent of python eval in Haskell

佐手、 提交于 2019-12-20 12:44:50
问题 There is function in python called eval that takes string input and evaluates it. >>> x = 1 >>> print eval('x+1') 2 >>> print eval('12 + 32') 44 >>> What is Haskell equivalent of eval function? 回答1: It is true that in Haskell, as in Java or C++ or similar languages, you can call out to the compiler, then dynamically load the code and execute it. However, this is generally heavy weight and almost never why people use eval() in other languages. People tend to use eval() in a language because