metaprogramming

Templating off of an arbitirary-length list of types in C++

Here's what I want to be able to type: class foo : public watchKeys<A, B, C> {}; //Or any list of keys Boost::mpl has sequences, which allow you to do this, but I don't want to have to do: class foo : public watchKeys<mpl::list<A, B, C> > {}; I don't mind it being "ugly" or verbose on the inside, but I want the way watchKeys is ult...

Objective-C class -> string like: [NSArray className] -> @"NSArray"

I am trying to get a string name of a class from the class object itself. // For instance [NSArray className]; // @"NSArray" I have found object_getClassName(id obj) but that requires an instance be passed to it, and in my case that is needless work. So how can I get a string from a class object, and not an instance? ...

Inheriting off of a list of templated classes, when supplied with the list of template arguments

I'm trying to write some metaprogramming code such that: Inheriting from some class foo<c1, c2, c3, ...> results in inheritance from key<c1>, key<c2>, key<c3>, ... The simplest approach doesn't quite work because you can't inherit from the same empty class more than once. Handling the "..." portion isn't pretty (since it's copy-pasta),...

Why can't I use a Perl variable's value to access a lexical variable name?

Why does this print 42: $answer = 42; $variable = "answer"; print ${$variable} . "\n"; but this doesn't: my $answer = 42; my $variable = "answer"; print ${$variable} . "\n"; ...

C++ metaprogramming with templates versus inlining.

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 ...

How to unroll a short loop in C++ using templates?

I wonder how to get something like this 1 write copy(a,b,2,3) 2 and then get, a[2]=b[2]; a[3]=b[3]; a[4]=b[4]; I know that C #defines can't be used recursively to get that effect, so I suppose that template meta-programming is in place. I know there is a boost library for that, but I only want that "simple" trick, and boost is ...

Find out the number of digits of min/max values of an integral type at compile time

Is there a way to find out the number of digits of min/max values of an integral type at compile time so that it's suitable to be placed as a template parameter? Ideally, there will be an existing solution in, for example, Boost MPL. Failing that I'm looking for some pointers for a hand-coded solution. ...

c++ recursive mpl::equal problem?

i need an mpl::equal like procedure that supports recursion on types. namespace mpl = boost::mpl; BOOST_MPL_ASSERT(( mpl::equal< mpl::vector<int, char>, typename mpl::push_back<mpl::vector<int>, char>::type > )); // OK the above compiles fine, however if i use it in mpl::transform or mpl::fold, visual studio 2010 rc1 complains. ...

Traversing Scheme function as a list

Isn't it possible to treat functions in Scheme as any other list? Basically, what I want do to is something like this: (define (foo) "hello") (cdr foo) ; or similar, should return the list ((foo) "hello") I've found a similar discussion about this, and I feel a bit disappointed if this is not possible with Scheme. If so, why is th...

How can I reverse ruby's include function.

I'll explain what i'm looking for in code as thats probably the most succinct: module Mixin def method puts "Foo" end end class Whatever include Mixin end w = Whatever.new w.method => "Foo" # some magic here w2 = Whatever.new w.method => NoMethodError I had tried just undefining the Mixin module using remove_const, but t...

How can I "replace" a module included via ruby include function

As a follow up to http://stackoverflow.com/questions/2403057/how-can-i-reverse-rubys-include-function, which was well answered but turned out my simplification of the real problem mean't that the solution was not applicable. I'm now faced with this (names changed to protect identities!): module OldFormHelpers def foo puts "foo" ...

Why can't I pass self as a named argument to an instance method in Python?

This works: >>> def bar(x, y): ... print x, y ... >>> bar(y=3, x=1) 1 3 And this works: >>> class Foo(object): ... def bar(self, x, y): ... print x, y ... >>> z = Foo() >>> z.bar(y=3, x=1) 1 3 And even this works: >>> Foo.bar(z, y=3, x=1) 1 3 But why doesn't this work? >>> Foo.bar(self=z, y=3, x=1) Traceback...

Is it possible to replace groovy method for existing object?

The following code tried to replace an existing method in a Groovy class: class A { void abc() { println "original" } } x= new A() x.abc() A.metaClass.abc={-> println "new" } x.abc() A.metaClass.methods.findAll{it.name=="abc"}.each { println "Method $it"} new A().abc() And it results in the following output: original or...

Ruby metaclass madness

I'm stuck. I'm trying to dynamically define a class method and I can't wrap my head around the ruby metaclass model. Consider the following class: class Example def self.meta; (class << self; self; end); end def self.class_instance; self; end end Example.class_instance.class # => Class Example.meta.class # => Class Ex...

How can I use functools.partial on multiple methods on an object, and freeze parameters out of order?

I find functools.partial to be extremely useful, but I would like to be able to freeze arguments out of order (the argument you want to freeze is not always the first one) and I'd like to be able to apply it to several methods on a class at once, to make a proxy object that has the same methods as the underlying object except with some o...

Is There a Good Pattern for Creating a Unique Id based on a Type?

I have a template that creates a unique identifier for each type it is instanced. Here's a streamlined version of the template: template <typename T> class arType { static const arType Id; // this will be unique for every instantiation of arType<>. } // Address of Id is used for identification. #define PA_TYPE_TAG(T) (&arType<T >::Id...

Generating tuples from tuples

Say you have a tuple and want to generate a new tuple by applying a metafunction on each type of the first one. What' the most efficient C++ metafuntion to accomplish to this task? Is it also possible to use C++0x variadic template to provide a better implementation? ...

Python: Class factory using user input as class names

Hi everyone, I want to add class atttributes to a superclass dynamically. Furthermore, I want to create classes that inherit from this superclass dynamically, and the name of those subclasses should depend on user input. There is a superclass "Unit", to which I can add attributes at runtime. This already works. def add_attr (cls, na...

Looking for Programming Language that allows you to change true and false.

For my curiosity sake I'm looking for a dynamic object oriented language that allows you to change true to false and vice versa. Something like this: true = false, false = true; This should also affect any conditional statements, therefore 42 == 42 should return False. Basically, with this premise, nothing in the language would be sa...

Cannot Generate ParameterSetMetadata While Programmatically Creating A Parameter Block

I'm trying to programmatically create a parameter block for a function ( along the lines of this blog post ). I'm starting with a CommandMetadata object (from an existing function). I can create the ParameterMetadata object and set things like the ParameterType, the name, as well as some attributes. The problem I'm running into is t...