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

Is there a smart way to implement an observer pattern completely static (at compile time)

EDIT: I want to implement an subject - observer pattern such that I can call notify() from anywhere and many update() fuctions get called A subject is a struct type. There can be several of these subjects I want to be able to put…
3
votes
1 answer

Macros vs functions that accept expressions

When I try the following code snippet, I got that the variable i is not found. Why is that ? function evalMyExpr(expr,n) for i in 1:n eval(expr) end end expr1 = Meta.parse("println(\"hello\")") expr2 =…
Antonello
  • 6,092
  • 3
  • 31
  • 56
3
votes
2 answers

Why template specialization doesn't work in gcc

I'd like to write typelist methods to operate with microcontrollers GPIO's. I'd like to create list of GPIO's and select only pins of specific port. So, GetPinWithPort template has specialisation which checks provided type. template
Dmitrey
  • 31
  • 1
3
votes
2 answers

Rails meta programming and performance

Rails conventions force framework and gem writers to make extensive use of reflection/meta programming. I've always assumed meta programming to be less efficient, and with the trend of using an ever growing number of gems, the question is - whats…
Syed Ali
  • 662
  • 1
  • 4
  • 15
3
votes
2 answers

Dynamic validation and metaprogramming in Ruby

I am trying to development an application that can present the same resource to different users and where the resource may have different validation behavior based on the user. I have tried to use Ruby metaprogramming to solve this in an easy way…
HakonB
  • 6,977
  • 1
  • 26
  • 27
3
votes
3 answers

Computing multiple results from one array iteration

I want to compute multiple functions in one iteration of an array or list, but might want to grow the list of functions dynamically. For example, I might want to compute min and max, and then, also want the average function (or any other linear…
3
votes
4 answers

Convert endianness of integer fields in struct using macros

Consider the following struct and functions typedef struct __attribute__((__packed__)) req_file { uint32_t start_pos; uint32_t byte_count; uint16_t name_len; } req_file; void req_file_hton(req_file *d){ d->name_len = htons(d->name_len); …
samvel1024
  • 1,123
  • 4
  • 15
  • 39
3
votes
5 answers

Why same types are different?

Why this (char is signed on my implementation): cout << std::is_same< char,signed char>::value; outputs false?
There is nothing we can do
  • 23,727
  • 30
  • 106
  • 194
3
votes
1 answer

Proof as an output argument in Prolog meta interpreter

I am putting together a simple meta interpreter which outputs the steps of a proof. I am having trouble with getting the proof steps as an output argument. My predicate explain1 returns the proof in the detailed form that i would like, but not as an…
user3170496
3
votes
3 answers

Ruby 2.6: How can I dynamically override instance methods when prepending a module?

I have a module called Notifier. module Notifier def self.prepended(host_class) host_class.extend(ClassMethods) end module ClassMethods def emit_after(*methods) methods.each do |method| define_method(method) do |thing,…
user3574603
  • 3,364
  • 3
  • 24
  • 59
3
votes
1 answer

template metaprogramming - g++ eats it, clang does not

Any way to get both compilers to be happy? for this: template struct static_signbits { enum { result = (!!(value & 0x8000) == !!(value & 0x4000)) ? (static_signbits::result + 1) : 0 }; }; template<> struct…
Phantasma
  • 55
  • 4
3
votes
0 answers

Control the environment of nested calls to source()

In this new set of features, I am trying to steer drake away from the user's global environment. This is challenging because users can define arbitrarily nested code files. Let's say a user defines files packages.R, functions.R, and master.R as…
landau
  • 5,636
  • 1
  • 22
  • 50
3
votes
2 answers

Metaprogramming sinatra get

I have a list of words in a list, and I want to handle get requests to any of them (and respond the same way). @words = ["foo","bar"....etc] One of the ways I thought I could do this is to loop through the list and have a get directive generated…
hsiu
  • 37
  • 4
3
votes
2 answers

Why is a naive `iter_swap`potentially much slower than `swap`?

From David Abrahams and Aleksey Gurtovoy's book "C++ Template Metaprogramming", I learned that iter_swap (see below) would be much slower than std::swap sometimes. Although the book has some explanation, I did not quite get it, could someone…
Long Gong
  • 54
  • 1
  • 3
3
votes
1 answer

Can I determine whether the type is a pointer type in C?

What do I know is that C has _Generic macros. One can even implement some limited typename() for ANSI C (just like it is mentioned here). But can I implement something like std::is_pointer in C? For example: #define IS_POINTER_TYPE(x) // ? struct…
Netherwire
  • 2,669
  • 3
  • 31
  • 54