Questions tagged [metaprogramming]

Metaprogramming is the capability to reprogram ones programming environment, as with macros or metaclasses.

Metaprogramming - the capability to reprogram ones programming environment - can save time and code by allowing one to create new abstractions, or new and more concise syntax for repetitive tasks.

Notable metaprogramming facilities include:

  • Common lisp macros
  • Scheme hygienic macros
  • Common lisp metaobject protocol
  • Python metaclass facility
  • Smalltalk's entirely transparent and reflective execution environment

For more info see http://en.wikipedia.org/wiki/Metaprogramming.

4722 questions
26
votes
5 answers

calculating factorial using template meta-programming

I do not understand how this piece of code (from Wikipedia) works: template struct Factorial { enum { value = N * Factorial::value }; }; template <> struct Factorial<0> { enum { value = 1 }; }; // Factorial<4>::value ==…
Lazer
  • 90,700
  • 113
  • 281
  • 364
26
votes
7 answers

Is it possible to create and initialize an array of values using template metaprogramming?

I want to be able to create an array of calculated values (let's say for simplicity's sake that I want each value to be the square of it's index) at compile time using template metaprogramming. Is this possible? How does each location in the…
aneccodeal
  • 8,531
  • 7
  • 45
  • 74
26
votes
4 answers

How to calculate offset of a class member at compile time?

Given a class definition in C++ class A { public: //methods definition .... private: int i; char *str; .... } Is it possible to calculate the offset of a class member at compile time using C++ template meta-programming? The…
Jun Yuan
  • 314
  • 1
  • 3
  • 5
26
votes
4 answers

Ruby String split with regex

This is Ruby 1.8.7 but should be same as for 1.9.x I am trying to split a string for example: a = "foo.bar.size.split('.').last" # trying to split into ["foo", "bar","split('.')","last"] Basically splitting it in commands it represents, I am trying…
Haris Krajina
  • 14,824
  • 12
  • 64
  • 81
25
votes
9 answers

Is there a hook similar to Class#inherited that's triggered only after a Ruby class definition?

#inherited is called right after the class Foo statement. I want something that'll run only after the end statement that closes the class declaration. Here's some code to exemplify what I need: class Class def inherited m puts "In #inherited…
kch
  • 77,385
  • 46
  • 136
  • 148
25
votes
3 answers

Programmatically determine whether exceptions are enabled

Most C++ compilers allow for exceptions to be disabled. Is there a way to determine it from the code without using compiler-specific preprocessor macros, such as _CPPUNWIND for MSVC? Ideally at compile time.
Nemanja Trifunovic
  • 24,346
  • 3
  • 50
  • 88
25
votes
1 answer

Assigning to an instance's __class__ attribute in Python

Under what circumstances is it possible, and when is it impossible, to assign to an instance's __class__ attribute in Python? Error messages such as TypeError: __class__ assignment: only for heap types don't really do it for me.
porgarmingduod
  • 7,668
  • 10
  • 50
  • 83
25
votes
4 answers

Python on Rails?

Would it be possible to translate the Ruby on Rails code base to Python? I think many people like Python more than Ruby, but find Ruby on Rails features better (as a whole) than the ones in Python web frameworks. So that, would it be possible? Or…
Juanjo Conti
  • 28,823
  • 42
  • 111
  • 133
25
votes
9 answers

__FILE__ macro manipulation handling at compile time

One of the issues I have had in porting some stuff from Solaris to Linux is that the Solaris compiler expands the macro __FILE__ during preprocessing to the file name (e.g. MyFile.cpp) whereas gcc on Linux expandeds out to the full path (e.g.…
ScaryAardvark
  • 2,855
  • 4
  • 30
  • 43
25
votes
9 answers

Template-ing a 'for' loop in C++?

I have a C++ snippet below with a run-time for loop, for(int i = 0; i < I; i++) for (int j = 0; j < J; j++) A( row(i,j), column(i,j) ) = f(i,j); The snippet is called repeatedly. The loop bounds 'I' and 'J' are known at compile time (I/J are…
RAC
  • 4,979
  • 3
  • 21
  • 10
25
votes
6 answers

How to get attributes that were defined through attr_reader or attr_accessor

Suppose I have a class A class A attr_accessor :x, :y def initialize(x,y) @x, @y = x, y end end How can I get x and y attributes without knowing how exactly they were named. E.g. a = A.new(5,10) a.attributes # => [5, 10]
user1179942
  • 381
  • 1
  • 4
  • 9
24
votes
11 answers

How do you debug heavily templated code in c++?

I find it very hard to figure out what is wrong with my code when using C++ template meta-programming. It might be that I am just not very good at understanding the error messages, but as far as I'm aware I can't resort to putting in print…
Dan
  • 33,953
  • 24
  • 61
  • 87
24
votes
2 answers

Swift: macro for __attribute__((section))

This is kind of a weird and un-Swift-thonic question, so bear with me. I want to do in Swift something like the same thing I'm currently doing in Objective-C/C++, so I'll start by describing that. I have some existing C++ code that defines a macro…
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
24
votes
15 answers

Non-C++ languages for generative programming?

C++ is probably the most popular language for static metaprogramming and Java doesn't support it. Are there any other languages besides C++ that support generative programming (programs that create programs)?
jwfearn
  • 28,781
  • 28
  • 95
  • 122
23
votes
7 answers

How to have an active binding know if it's called as a function?

I would like something like that: makeActiveBinding("f", function() { called_as_a_function <- ... # <- insert answer here if(called_as_a_function) { sqrt } else { 1 } }, .GlobalEnv) # Expected output f + f #> 2 f(4) + f #> 3 I…
moodymudskipper
  • 46,417
  • 11
  • 121
  • 167