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
32
votes
4 answers

Is there a way to do a C++ style compile-time assertion to determine machine's endianness?

I have some low level serialization code that is templated, and I need to know the system's endianness at compiletime obviously (because the templates specializes based on the system's endianness). Right now I have a header with some platform…
Robert Gould
  • 68,773
  • 61
  • 187
  • 272
32
votes
2 answers

Using yield inside define_method in Ruby

Is it possible to make yield keyword work inside a block given to define_method? Simple example: class Test define_method :test do |&b| puts b # => # yield end end Test.new.test { puts "Hi!" } This code produces…
Dawid
  • 4,042
  • 2
  • 27
  • 30
32
votes
4 answers

SFINAE to check for inherited member functions

Using SFINAE, i can detect wether a given class has a certain member function. But what if i want to test for inherited member functions? The following does not work in VC8 and GCC4 (i.e. detects that A has a member function foo(), but not that B…
Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
32
votes
5 answers

Overriding special methods on an instance

I hope someone can answer this that has a good deep understanding of Python :) Consider the following code: >>> class A(object): ... pass ... >>> def __repr__(self): ... return "A" ... >>> from types import MethodType >>> a = A() >>>…
James Mills
  • 18,669
  • 3
  • 49
  • 62
31
votes
2 answers

Quick sort at compilation time using C++11 variadic templates

I just implemented the quick sort algorithm by using C++11 variadic templates to evaluate it at compilation time. However, I encounter a performance issue when the data set is too large. #include using namespace std; template
Yuncy
  • 761
  • 1
  • 9
  • 20
31
votes
4 answers

Python equivalent of Ruby's 'method_missing'

What is Python's equivalent of Ruby's method_missing method? I tried using __getattr__ but this hook applies to fields too. I only want to intercept the method invocations. What is the Python way to do it?
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
31
votes
4 answers

Why is `object` an instance of `type` and `type` an instance of `object`?

I am a little bit confused about the object and type classes in Python 3. Maybe someone can clear up my confusion or provide some additional information. My current understanding is that every class (except object) inherits from a base class called…
felixinho
  • 625
  • 7
  • 17
31
votes
8 answers

How can I simulate macros in JavaScript?

I know that JavaScript doesn't support macros (Lisp-style ones) but I was wondering if anyone had a solution to maybe simulate macros? I Googled it, and one of the solutions suggested using eval(), but as he said, would be quite costly. They don't…
Anders Rune Jensen
  • 3,758
  • 2
  • 42
  • 53
31
votes
5 answers

Why compile error with enable_if

Why this does not compile with gcc48 and clang32? #include template struct S { template typename std::enable_if::type f(T t) {return 1;}; template typename…
Leonid Volnitsky
  • 8,854
  • 5
  • 38
  • 53
31
votes
11 answers

Template Metaprogramming - I still don't get it :(

I have a problem... I don't understand template metaprogramming. The problem is, that I’ve read a lot about it, but it still doesn’t make much sense to me. Fact nr.1: Template Metaprogramming is faster template struct Factorial { enum {…
n00ki3
  • 14,529
  • 18
  • 56
  • 65
30
votes
3 answers

Dynamically define named classes in Ruby

I am writing an internal DSL in Ruby. For this, I need to programmatically create named classes and nested classes. What is the best way to do so? I recon that there are two ways to do so: Use Class.new to create an anonymous class, then use…
Little Bobby Tables
  • 5,261
  • 2
  • 39
  • 49
30
votes
2 answers

Modify default queryset in django

I have added a 'cancelled' field to my model, is there a way to modify the model default query to something like cancelled=False ? without having to modify all my filter/exclude queries ?
juanefren
  • 2,818
  • 6
  • 32
  • 41
30
votes
3 answers

How to use define_method inside initialize()

Trying to use define_method inside initialize but getting undefined_method define_method. What am I doing wrong? class C def initialize(n) define_method ("#{n}") { puts "some method #{n}" } end end C.new("abc") #=> NoMethodError:…
Bala
  • 11,068
  • 19
  • 67
  • 120
30
votes
2 answers

Can someone explain the Class.superclass.class.superclass paradox?

It's probably not a paradox at all, but from a newbies perspective, it sure seems that way. > Class.superclass => Module > Class.superclass.class => Class > Class.superclass.class.superclass => Module So a class's parent is module, but module is a…
Nathan
  • 7,627
  • 11
  • 46
  • 80
29
votes
3 answers

C++ metafunction to determine whether a type is callable

Is it possible to write a C++(0x) metafunction that determines whether a type is callable? By callable type I mean a function type, function pointer type, function reference type (these are detected by boost::function_types::is_callable_builtin),…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194