metaprogramming

How to use for_each here correctly?

Hi, I am new to all the functional stuff in STL. I tried to do smething but it fails horribly no matter how I try it. Please comment: #include <iostream> #include <algorithm> using namespace std; class X { public: void Print(int x) { cout << x << endl; } void Do() { ...

How do I determine the number of bits in a template non-type integral constant parameter?

I would assume that this is covered in the C++ Standard, but I've not been able to find it. I am writing some templates that are going to do arithmetic on their non-type integral parameters, and I find I need the equivalent of MAX_INT for the parameter 'x' in a template like template <int x> Foo. Ideally someone could point me to the pa...

Generic programming v.s. Metaprogramming...

What exactly is the difference? It seems like the terms can be used somewhat interchangeably, but reading the wikipedia entry for Objective-c, I came across: In addition to C’s style of procedural programming, C++ directly supports certain forms of object-oriented programming, generic programming, and metaprogramming. in r...

How can I programmatically determine which class/module defines a method being called?

In Ruby, how can I programmatically determine which class/module defines a method being called? Say in a given scope I invoke some_method(). In the same scope I'd like to invoke a function find_method(:some_method) that would return which Class, Singleton Class or Module defines some_method. Here's some code to illustrate what I mean:...

Rails metaprogramming and validations

Given a particular model, is there a way to see and edit existing validations? I'm writing a plugin and I need to modify a validation if it exists. For example: If I have a User model with validate_uniqueness_of :ssn This model is using the plugin so I need to modify that validation and add an :if or a :scope, etc. So basically if ...

Why does a local variable lose its value when defining a method with define_method?

Trying to follow along with a metaprogramming screencast from pragpub and ran into some problems because of changes in Ruby since the release of screencast. Hard to explain the problem w/o the code, so here's that: class Discounter def discount(*skus) expensive_discount_calculation(*skus) end private def expensive_discou...

What is the best way to store procedures/code?

Hello, I have a feed reader written in Rails (the logic is bit complex as I am scraping some data) and I am trying to generalize the methods. Here is my current structure - Class Gizmodo def update update logic end end Class Wired def update update logic end end Now I am thinking of structure like this Cl...

Turing machine: But why use template metaprogramming ?

Hi there. I am a final year engineering student. Me and my friends have decided that our final year project would be "Simulation of Turing Machine using Template Metaprogramming". I understand what "Turing Machine" and "Template Metaprogramming" are but my question is why the simulation would be tedious if we design the Turing Machine ...

A std::map like container that maps types to values.

I'm looking for a hybrid meta-container/container class. I want a class that maps a compile-time type to a runtime value. A code snippit is worth 1024 words so: struct Foo { /* ... */ }; struct Bar { /* ... */ }; int main() { meta_container<Foo,float,int> mc; mc.get<float>() = 1.0f; mc.get<Foo>().method(blah); mc.get<Bar>...

Type decision based on existence of nested typedef.

I need to define a template struct such that: element<T>::type is of type: T::element_type if T contains a (public) typedef named element_type, otherwise (if it does not contain such typedef) element<T>::type is of type T::value_type if T is mutable and of type const T::value_type if T is constant. I am really strugglin...

Advantages of using boost::mpl::bool_ instead of a const bool

I am confused about the advantages of using the the bool_<true> and bool_<false> types against simply using const bools in the context of template metaprogramming. The boost::mpl library clearly prefers the first approach, and defines helper functions like and_, or_ to help manage such bool_. Conditional metafunctions like if_...

c++ metaprogramming madness

consider the following templated datastructures enum eContent{ EINT = 1, EFLOAT = 2, EBOOL = 4 }; template<int> struct Container{ Container(){assert(false);} //woops, don't do that! }; template<> struct Container<EINT>{ Container():i(123){} int i; }; template<> struct Container<EFLOAT>{ Container():f(123.4...

metaprogramming access local variables

class Foo def initialize bar = 10 end fiz = 5 end Is there a possibility to get these local values (outside the class) ? ...

c++ fatal error C1061 with large switch, metaprogramming

that's the code: static inline void shrinkData(const vector<Data> &data, unsigned short shrinkType){ #define CASE_N(N) \ case(N): \ ptr = MemoryManager::requestMemory(n*sizeof(ShrinkData<N>)); \ for(int i=0; i<n; i++){ \ new(ptr) ShrinkData<N>(data[i]); \ ptr+=sizeof(ShrinkData<N>); \ ...

Turning boost::tuples::cons<...> back into the corresponding boost::tuple<...>

For a little library project I'm using boost::tuple. Right now, I'm facing the problem of turning a "cons list" I operated on via metaprogramming back to a boost::tuple<...> type. The "dirty" solution would be to provide lots of partial specialications a la template<class T> struct id{typedef T type;}; template<class TL> struct type_li...

boost_assert that a parameter class implements a certain method

Hi, Suppose you have a certain template that takes a parameter class template <typename ConnectorClass> struct myClass { } I want to add a BOOST_ASSERT_MSG to validate that ConnectorClass implements a certain method of signature returnType MethodName(param1, param2) How should i write the assert condition in this case? EDIT: si...

Augmenting a model from an external gem

I'm using refinerycms in our site to let the less technical staff update content. Inside the gem, they have a Page class that maps each top level page on the site. I'd like to use the acts_as_taggable gem on this Page class. Now I can add the acts_as_taggle declaration directly to the page.rb file, but then I'd have to maintain a separat...

method compile time assertion; still not working

I need a easy way to assert inside a template that a template parameter implements a method (or one of its parent classes). I've read Concept check library but is hard to find an easy example to do simple checks like this one. I've tried to follow other posts (like this one and this other one), which i've modified so i can make it gener...

Functional C# - using or returning Action's

Browsing the net for better fault handling in C#, I've com across the following to implementation strategies. The first one is natural to me, while the other implementation I'm not certain what its advantages are? 1) static void Fault(Action protectedBlock, Action faultHandler) { try { protectedBlock(); } catc...

How do you override a method for a java type instance with Groovy meta programming?

Hi, I am trying to override the functionality of a method of a java type instance in my Groovy code but I am getting a classcast exception. I looked at the guide posted here but I can not get it to work. Since my actual problem is a bit of mess, below is some runnable example code that fails with the same error. In the example I wa...