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
45
votes
3 answers

Is constexpr-based computation Turing complete?

We know that C++ template metaprogramming is Turing complete, but preprocessor metaprogramming is not. C++11 gives us a new form of metaprogramming: computation of constexpr functions. Is this form of computation Turing-complete? I am thinking that…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
45
votes
4 answers

Why is partial specialization of a nested class template allowed, while complete isn't?

template struct A { template struct B {};. …
Gunther Piez
  • 29,760
  • 6
  • 71
  • 103
44
votes
2 answers

How do I get constants defined by Ruby's Module class via reflection?

I was trying to get Matz and Flanagan's "Ruby Programming Language" metaprogramming chapter into my head, However I couldn't understand the output from the following code snippet that I dreamed up: p Module.constants.length # =>…
Gishu
  • 134,492
  • 47
  • 225
  • 308
42
votes
9 answers

Enumerate over an enum in C++

In C++, Is it possible to enumerate over an enum (either runtime or compile time (preferred)) and call functions/generate code for each iteration? Sample use case: enum abc { start a, b, c, end } for each…
jameszhao00
  • 7,213
  • 15
  • 62
  • 112
42
votes
5 answers

What is the meaning of “quasi” in quasiquotations?

Some languages like Haskell (or Nemerle) have quasiquotations. I wonder what the “quasi” means and if there also exist “quotations” without the “quasi” part.
soc
  • 27,983
  • 20
  • 111
  • 215
41
votes
6 answers

Executing code for every method call in a Ruby module

I'm writing a module in Ruby 1.9.2 that defines several methods. When any of these methods is called, I want each of them to execute a certain statement first. module MyModule def go_forth a re-used statement # code particular to this…
GladstoneKeep
  • 3,832
  • 2
  • 26
  • 38
41
votes
3 answers

define_method: How to dynamically create methods with arguments

I want to create a bunch of methods for a find_by feature. I don't want to write the same thing over and over again so I want to use metaprogramming. Say I want to create a method for finding by name, accepting the name as an argument. How would I…
Kostas Andrianos
  • 1,551
  • 2
  • 16
  • 21
40
votes
8 answers

Adding an instance variable to a class in Ruby

How can I add an instance variable to a defined class at runtime, and later get and set its value from outside of the class? I'm looking for a metaprogramming solution that allows me to modify the class instance at runtime instead of modifying the…
readonly
  • 343,444
  • 107
  • 203
  • 205
40
votes
10 answers

Is static metaprogramming possible in Java?

I am a fan of static metaprogramming in C++. I know Java now has generics. Does this mean that static metaprogramming (i.e., compile-time program execution) is possible in Java? If so, can anyone recommend any good resources where one can learn…
jwfearn
  • 28,781
  • 28
  • 95
  • 122
39
votes
7 answers

Ruby String to Class Name

I am trying to create a new class to that will inherit from ActiveRecord::Base the class needs to be dynamically generated from a string "general_systems".camelize.singularize = Class.new < ActiveRecord::Base However I keep getting the…
Telmo
  • 391
  • 1
  • 3
  • 3
39
votes
1 answer

What does the tt metavariable type mean in Rust macros?

I'm reading a book about Rust, and start playing with Rust macros. All metavariable types are explained there and have examples, except the last one – tt. According to the book, it is a “a single token tree”. I'm curious, what is it and what is it…
Pavel Davydov
  • 3,379
  • 3
  • 28
  • 41
39
votes
9 answers

Is metaprogramming possible in C#?

In particular, would it be possible to have code similar to this c++ code executed at compile time in c#? template struct Factorial { enum { value = N * Factorial::value }; }; template <> struct Factorial<0> { enum { value…
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
39
votes
8 answers

Find out whether a C++ object is callable

Is it possible to write a type trait, say is_callable which tells if an object has an operator() defined? It is easy if the arguments to the call operator are known in advance, but not in the general case. I want the trait to return true if and…
Antoine
  • 13,494
  • 6
  • 40
  • 52
39
votes
2 answers

How to pass arguments to the metaclass from the class definition?

I'm trying to dynamically generate classes in python 2.7, and am wondering if you can easily pass arguments to the metaclass from the class object. I've read this post, which is awesome, but doesn't quite answer the question. at the moment I am…
yarbelk
  • 7,215
  • 6
  • 29
  • 37
38
votes
4 answers

GCC error with variadic templates: "Sorry, unimplemented: cannot expand 'Identifier...' into a fixed-length argument list"

While doing variadic template programming in C++11 on GCC, once in a while I get an error that says "Sorry, unimplemented: cannot expand 'Identifier...' into a fixed-length arugment list." If I remove the "..." in the code then I get a different…
Dennis
  • 2,607
  • 3
  • 21
  • 28