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

typedef Using a typename

So I had some code that I asked a question about which I realized was confusing and later edited out: template struct foo { typedef typename pair PointType; private: PointType point; }; I'm not certain what the function…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
3
votes
2 answers

Checking if a function with a given signature exists in c++

So I was looking for ways to check whether a function with a particular argument exists. I have a templated method which relies on an external function (external from the class) to do the job: template void exportDataTo(Moo& ret){ …
kamziro
  • 7,882
  • 9
  • 55
  • 78
3
votes
4 answers

How to pass filename and file content to awk

tl;dr Does anyone know how to pass a filename and the rest of the content of the file to awk? And make it run through all files in the directory and append the output of those actions to 1 final file? long story: I need to generate an SQL update…
Willem Dauwen
  • 31
  • 1
  • 3
3
votes
1 answer

Ruby Kernel module methods used as Object class methods

Say I want to patch the Kernel module with one method I just came up with: module Kernel def say_hello puts "hello world" end end I can now certainly do this: Object.new.say_hello # => hello world but I can also do the following which I…
stratis
  • 7,750
  • 13
  • 53
  • 94
3
votes
2 answers

Changing Object.toString() dynamically in Groovy has no effect when calling Integer.toString()

I injected overridden method toString into Object.metaClass: Object.metaClass.toString ={ System.out.println("the string is $delegate") } and I thought that following code will execute this method: 1500.toString() But it didn't not,nothing was…
KyrinWoo
  • 327
  • 1
  • 3
  • 9
3
votes
1 answer

Runtime class creation in Rails. How should I handle this?

I'm working on a reporting and logging system that'll act as a viewport on the statistics of other applications. I want the ORM functionality of ActiveRecord, but do not have the DB structures of apps before hand. The extra databases are defined in…
EarlyPoster
  • 1,068
  • 1
  • 11
  • 27
3
votes
3 answers

Creating serializeable unique compile-time identifiers for arbitrary UDT's

I would like a generic way to create unique compile-time identifiers for any C++ user defined types. for example: unique_id::value == 0 // true unique_id::value == 1 // true I've managed to implement something like this…
Endiannes
  • 53
  • 5
3
votes
2 answers

Create an alias from one type to another

I'm writing some kind of DI container in c++ and I'm curious if it's possible to create aliases from one type to another in modern c++. What I basicly want to do is to be able to call implementation constructor by it's aliased interface. Like…
s0nicYouth
  • 470
  • 3
  • 15
3
votes
1 answer

Dynamically defining methods with names derived from a class attribute?

The title might not be the best description of what I'm trying to do but I'm not sure what to call this. I came across various seemingly related concepts with names like "decorators", "descriptors", and "metaclasses" but I don't know which of those…
tino
  • 4,780
  • 5
  • 24
  • 30
3
votes
4 answers

Find the type of the elements from the type of the array

Suppose I have a type T which is guaranteed to be an array (eg. int[32]). How do I get the type of the elements from the type of the array? I found a way to do it, but it involves declaring a dummy temporary variable, which I would like to…
SlySherZ
  • 1,631
  • 1
  • 16
  • 25
3
votes
4 answers

compile time templated C++ calculations on unsigned long long ? on doubles?

I use compile-time int powers calculation Power, to compute n**p. It uses ints. I want to calculate something that's bigger than int, ok ? It fits into u64 (unsigned long long). Can C++ templates do compile-time calculations on u64 ? Enums cannot do…
Andrei
  • 8,606
  • 10
  • 35
  • 43
3
votes
3 answers

Why is my define_method content being evaluated in the class scope?

I'm doing some (too) fancy meta programming, and I have a hard time understanding why the scope is different in the following two cases: Case 1: class TesterA def the_method puts "I'm an instance_method!" end def self.the_method puts…
Niels Kristian
  • 8,661
  • 11
  • 59
  • 117
3
votes
1 answer

How does clang detect noexcept-ness?

I'm currently reimplementing std::invoke in C++11 (i.e. understanding and adapting libc++/libstdc++ code), and I stumbled upon an issue related to noexcept. This can be demonstrated with the following snippet: #include void…
Dante
  • 404
  • 2
  • 10
3
votes
3 answers

Issues with a C++ metafunction to detect if a function exists

I've got a problem with a C++ meta-function that I don't understand. I'm compiling on Apple's build of clang 8.1.0, using C++14. Working code that illustrates the problem is below. I've cribbed a metafunction from elsewhere and I'm trying to use…
3
votes
2 answers

Check if a function has keywords arguments in Julia

Is there a way to check if a function has keywords arguments in Julia? I am looking for something like has_kwargs(fun::Function) that would return true if fun has a method with keyword arguments. The high level idea is to build a function: function…
Maxime
  • 56
  • 1
  • 4