847 questions
148
votes
6
answers
38k
views
Is it possible to figure out the parameter type and return type of a lambda?
Given a lambda, is it possible to figure out it's parameter type and return type? If yes, how?
Basically, I want lambda_traits which can be used in following ways:
auto lambda = [](int i) { return ...
34
votes
5
answers
10k
views
Overriding special methods on an instance
Consider the following code:
>>> class A(object):
... pass
...
>>> def __repr__(self):
... return "A"
...
>>> from types import MethodType
>>> a = ...
117
votes
5
answers
91k
views
How to call methods dynamically based on their name? [duplicate]
How can I call a method dynamically when its name is contained in a string variable? For example:
class MyClass
def foo; end
def bar; end
end
obj = MyClass.new
str = get_data_from_user # e.g. `...
179
votes
5
answers
83k
views
Is it possible to implement dynamic getters/setters in JavaScript?
I am aware of how to create getters and setters for properties whose names one already knows, by doing something like this:
// A trivial example:
function MyObject(val){
this.count = 0;
this....
80
votes
12
answers
40k
views
Does C++ support compile-time counters?
For the purpose of introspection, sometimes I've wanted to automatically assign serial numbers to types, or something similar.
Unfortunately, template metaprogramming is essentially a functional ...
166
votes
21
answers
113k
views
Conveniently Declaring Compile-Time Strings in C++
Being able to create and manipulate strings during compile-time in C++ has several useful applications. Although it is possible to create compile-time strings in C++, the process is very cumbersome, ...
119
votes
12
answers
72k
views
Compile time string hashing
I have read in few different places that using C++11's new string literals it might be possible to compute a string's hash at compile time. However, no one seems to be ready to come out and say that ...
63
votes
1
answer
5k
views
Is stateful metaprogramming ill-formed (yet)?
One of my most beloved/evil inventions I've had the fortune to come across is the constexpr counter, aka stateful metaprogramming. As mentioned in the post, it seems to be legal under C++14, and I'm ...
79
votes
14
answers
50k
views
Programmatically create static arrays at compile time in C++
One can define a static array at compile time as follows:
const std::size_t size = 5;
unsigned int list[size] = { 1, 2, 3, 4, 5 };
Question 1 - Is it possible by using various kinds of ...
526
votes
21
answers
845k
views
Python dictionary from an object's fields
Do you know if there is a built-in function to build a dictionary from an arbitrary object? I'd like to do something like this:
>>> class Foo:
... bar = 'hello'
... baz = 'world'
...
...
230
votes
5
answers
86k
views
Get the name of the currently executing method
$0 is the variable for the top level Ruby program, but is there one for the current method?
32
votes
4
answers
15k
views
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 ...
55
votes
8
answers
15k
views
Does Javascript have something like Ruby's method_missing feature?
In Ruby I think you can call a method that hasn't been defined and yet capture the name of the method called and do processing of this method at runtime.
Can Javascript do the same kind of thing ?
181
votes
10
answers
84k
views
What are good uses of SFINAE?
I want to get into more template meta-programming. I know that SFINAE stands for "substitution failure is not an error." But can someone show me a good use for SFINAE?
42
votes
6
answers
27k
views
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-...