Questions tagged [metaprogramming]
Metaprogramming is the capability to reprogram ones programming environment, as with macros or metaclasses.
4,489
questions
-1
votes
1
answer
30
views
Any way to build an array of pointers from a tuple of different objects (but derived from the same base class)?
Good morning all!
reading stack overflow for a long time, but this is my first post here.
For some reasons I would like to do something like this:
class Base{
...
}
class A : public Base{
...
}
...
0
votes
1
answer
21
views
call variadic template function on items in tuple of arrays
Let's say I have a function
template<typename retScalar, typename... scalars>
retScalar func_scalar(scalars... items);
declared somewhere.
I now want a "array" version of this ...
0
votes
0
answers
42
views
Detecting `std::shared_ptr` malfunction
I am starting to write a simple SQLite wrapper.
One of the functions is
template <typename... t_fields>
void exec(const char *p_sql, t_fields &&... p_bindings) {
sqlite3_stmt *...
0
votes
0
answers
15
views
C++ Template Meta-programming: having rouble implementing implementing binary tree [duplicate]
I'm attempting to write a template meta-programming program to implement a Binary Tree struct, with a method "Map" that applies a metafunction to every node in the tree. For instance, if I ...
0
votes
1
answer
29
views
Template function deduction fail on std::conditional argument
Please, before marking this as a duplicate of This question read the entirety of the post
This piece of code fails to compile, with a template deduction error:
#include <iostream>
#include <...
1
vote
1
answer
19
views
Creating an automatic delegating wrapper class in Python 3
I created a delegating wrapper class in Python 3(.8), which allow me to fully replicate the base-class' API, delegate to the underlying instance, and swap the underlying instance when necessary.
I'm ...
5
votes
2
answers
126
views
Why is returning a const from a function not being detected as a const?
I have a program which depends on the result of std::is_same_v <const value_t, decltype(value)>. However, I have found that when functions are passed to this expression the result is unexpected, ...
0
votes
1
answer
39
views
decouple member variables of a struct in variadic function accordingly
I have posted a question before, unpack variadic arguments and pass it's elements accordingly. However, it didn't quite address my problem as I am not asking it precisely. Hence I would like to ...
1
vote
1
answer
57
views
unpack variadic arguments and pass it's elements accordingly
suppose I got a struct i.e. Coord that contains two static member variables, then pass it as an argument of variadic template function variadic_print_coord(), how do I unpack the variadic expressions, ...
0
votes
1
answer
92
views
How can I incrementally create an array of types?
template<typename ...Args>
class TypesArray {
std::tuple<Args...> *tuple_;
template<typename T>
struct push_back_s {
using type = TypesArray<Args..., T>;
...
6
votes
1
answer
183
views
Python Enum and Pydantic : accept enum member's composition
I have an enum :
from enum import Enum
class MyEnum(Enum):
val1 = "val1"
val2 = "val2"
val3 = "val3"
I would like to validate a pydantic field based on that ...
3
votes
1
answer
41
views
How can I get argument values for a given lambda from the outside, without explicitly returning its `binding`?
I want to be able to run a lambda and get at its argument values (the values of a and b below).
I can achieve this by explicitly having the lambda return a binding, and then getting the values out of ...
0
votes
0
answers
29
views
How to set a confluence metadata value in Python?
I am trying to set/update a metadata value using a Python script, but I don't know how to access that specific field. For e.g. I have this Confluence page and I would like to change the metadatafield....
0
votes
0
answers
54
views
Generate std::array types and store them for runtime use
I have a wrapper class which is templated on the container type I am using, which may be an std::array of some size:
//! @tparam C the random access container type to be wrapped. E.g.
//! std::...
0
votes
1
answer
37
views
How to get a removed-reference tuple type without using an instance in C++
I wrote a test framework for testing function output (see code below).
template <class FuncTy, class... IndexType>
typename std::enable_if<std::tuple_size<typename function_helper&...
0
votes
1
answer
54
views
How do I access an object field via its field name in string form?
I'm figuring out at compile-time the name of a specific field of an object that I want to access. Before I compile, I do not necessarily know which field that is going to be, so I just have that field ...
0
votes
0
answers
41
views
Use OCamlFormat as a library
I would like to use ocamlformat as a library to automatically format some OCaml code I generate. There does not seem to be a simple function format : string -> string, but maybe there is a way to ...
0
votes
0
answers
21
views
Class template argument deduction with non-deducible argument [duplicate]
I'm trying to use class template argument deduction while having non-deducible argument, kinda expected that it would work automatically/implicitly, but it doesn't, seems so simple but i just can't ...
0
votes
1
answer
25
views
Restrict Kafka consumers based on event headers (metadata)
The book "Building Event-Driven Microservices" gives good practice to use a metatags (event headers) for placing restrictions on Kafka consumers. One of which is the following:
Deprecation:
...
1
vote
3
answers
52
views
Check for existence of nested type alias and conditionally set type alias
I wonder how to conditionally set a type alias, based on the existance of a type alias in input argument like this.
struct a { using type = int; }
template <typename T> struct wrapper {
...
1
vote
2
answers
58
views
Ruby, How I can obtain the whole namespace definition path for a method inside the same method?
How I can obtain the whole namespace definition path for a method inside the same method?
Using this example to make easy the question:
module Parent
module Child
def self....
3
votes
1
answer
61
views
R metaprogramming: pass expression/quosure to function that partially accesses local frame
I will use the following example to explain my question. But the question is not only about this specific example, but more general about meta-programming in R.
I have two specific functions to make ...
0
votes
0
answers
23
views
How to generate function's names dynamically from the type declaration files?
I wish typescript had better(any) macros!
I'm trying to dynamically generate an interface to existing (but unknown) code given a set of type declaration files. These .d.ts typedefs are generated by ...
7
votes
0
answers
90
views
In multi-stage compilation, should we use a standard serialisation method to ship objects through stages?
This question is formulated in Scala 3/Dotty but should be generalised to any language NOT in MetaML family.
The Scala 3 macro tutorial:
https://docs.scala-lang.org/scala3/reference/metaprogramming/...
1
vote
1
answer
85
views
When should I be using if constexpr as apposed to a regular if in a constexpr template function?
I've been trying to write a metafuncion to evaluate powers at compile time.
I have managed to do it with template metaprogramming, implemented as such:
template<int A, int B>
struct pow {
...
7
votes
0
answers
101
views
Why don't types with invalid inheritance get SFINAE-out with void_t?
Suppose we have a current class:
template<typename T>
struct Inheriter : public T {};
Note that its instantiation would be well-formed only if T is a class/struct that is not declared as final.
...
0
votes
1
answer
36
views
Creating a Python type(class) with methods as callable objects
For example, if I do this for a classmethod everything goes as expected and the actual cls gets passed to the X.__call___:
class X:
def __call__(self, *args, **kwargs):
print(self)
...
2
votes
0
answers
69
views
How can I give a compile error if the function is evaluated at compile time and a runtime exception otherwise?
If I have a constexpr function that can raise an exception at runtime, I want to make it able to give a compile error if it's evaluated at compile time.
#include <stdexcept>
#include <...
1
vote
1
answer
56
views
Implementation of typed tuple wrapper
How can an implementation look like, that wraps around e.g. a std::tuple as a static list of type/value, plus a type (not contained in tuple) to refer to some kind of owner/visitor.
I want to ...
0
votes
1
answer
46
views
How to access class method from the included hook of a Ruby module
I'd like my module to define new instance methods based on its including class' instance methods. But in the included hook, the class methods are not defined yet (as the module is included at the top ...
0
votes
1
answer
33
views
Python: Protocol metaclass with read-only properties
I want to achieve a Protocol with read-only properties with reduced boilerplate. In short:
# I want to reduce boilerplate of the following.
class MyVerboseProtocol(Protocol)
@property
def x(...
0
votes
1
answer
13
views
rlang::parse_expr with string with escape characters
how I could use eval(rlang::parse_expr(string))’ or alternative with such expresssion string <-"print('A\s*B')"`? I am getting unrecognized escape character. The expression is evaluated ...
1
vote
1
answer
67
views
Recursive iteration over type lists and concatenation into a result type list
Consider a scenario having various classes/structs, some having complex data members, which can contain more of them itself. In order to setup / initialize, a list of all dependencies is required ...
9
votes
3
answers
360
views
Why don't types with invalid inheritance get rejected when passed as template parameters?
As we all know, classes can't be inherited from fundamental types and from classes that are marked as final. But despite that, the code presented below compiles without any problems on Clang 12 and ...
1
vote
1
answer
22
views
How/When is the Groovy MetaClassRegistry populated?
I read that every POJO used in Groovy gets a MetaClass associated with it. These meta-classes are stored in the application wide metaclass registry. How are these meta-classes generated and placed in ...
1
vote
0
answers
34
views
Is there a way to write dune files programatically from OCaml?
I am generating OCaml code in OCaml. This code includes several Dune files which may be updated and have specific parts change. I am reaching the limit of what I can do with some clever printf, and ...
0
votes
0
answers
63
views
scala 3 macro: get class properties
i want to writing a macro to get property names of a class.
but can not use Symbol module in quoted statement. i receive blow error...
inline def getProps(inline className: String): Iterable[String] = ...
0
votes
1
answer
62
views
How to implement zero-overhead Inversion of Control
Almost every OOP programmer has been exposed to the concept of Inversion of control. In C++, we can implement that principle with dynamic callbacks (i.e. functors such as lambdas and function pointers)...
0
votes
2
answers
33
views
DRY Multiple same associations in rails Model
In my model, I have multiple has_one associations like
has_one :t1_for_self_order, -> { t1_for_self_order }, as: :source, dependent: :destroy, inverse_of: :source,
class_name: 'Spree::...
0
votes
1
answer
136
views
C++ template and std::any how to execute any generic function with generic parameters of different length and type
#include <iostream>
#include <vector>
#include <string>
#include <type_traits>
#include <map>
#include <any>
using namespace std;
int func1(int a, int b, int c) {
...
2
votes
1
answer
57
views
Detect the existence of a template instantiation for a given type
I'm using templates to explicitly declare and allow read access to specific data.
#include <type_traits>
template <typename T>
struct Access
{
template <typename U>
void ...
1
vote
2
answers
62
views
Can type be created(or template instantiated) by object known at compile time?
Suppose I have a template function:
template <typename T, T value>
auto foo(std::integral_constant<T, value>)
{
if constexpr (value == 0)
{
return int{};
}
...
1
vote
1
answer
46
views
Low-overhead tracing function in Python by modify the code object
sys.settrace is inefficient, to say the least. It adds a lot of overhead to every function call in Python.
Instead, I'm looking for a way to trace the "call" event for only a couple hundred ...
2
votes
0
answers
49
views
API docs for a generic controller that handles all requests
I need to generate api endpoints for certain database views we have in our postgres database dynamically, as we may add / delete views we don't want to update the code every time we do this.
for this ...
0
votes
1
answer
88
views
Deducing inheritance relationships in a list of classes
Given a list of classes, I need to find the inheritance relationships between the classes. For example:
class Animal {};
class Dog : public Animal {};
class Bulldog : public Dog {};
class Cat : public ...
0
votes
2
answers
90
views
It is possible to assign an object to another with a different type
I'm working with a C++ project and I need to do some assignment code to assign one object to another with a different type like this:
MyClass1 o1;
MyClass2 o2;
o2 = o1;
Ofc, we can make this work ...
0
votes
1
answer
43
views
Create dynamically methods in a module for each constant value defined within the module
I am trying to find out a way to generate dynamically methods in module (SomeConstants) based on constants defined within this module.
what I think I would like to achieve is the situation in which I ...
1
vote
2
answers
84
views
Is it possible to check if two classes have the same members
struct Test1 : public Base {
enum { type = 1 };
int a;
char ch;
virtual void func1();
};
struct Test2 : public Base {
enum { type = 2 };
int a;
char ch;
virtual void ...
1
vote
1
answer
61
views
Use the value of strongly typed enum as index in boost::mpl::map
I am using a C++ map structure defined similar to std::map<Foo, std::any> for storing attributes of a compiler symbol table. The Foo is a strongly typed enum, such as enum class Foo {ENUM}. ...
0
votes
0
answers
44
views
get annotations from class in scala 3 macros
i am writing a macro to get annotations from a 'Class'
inline def getAnnotations(clazz: Class[?]): Seq[Any] = ${ getAnnotationsImpl('clazz) }
def getAnnotationsImpl(expr: Expr[Class[?]])(using Quotes):...