metaprogramming

C++ template partial specialization with inheritance

倖福魔咒の 提交于 2020-01-02 07:31:29
问题 I need partial specialization of the struct , but I'd like also use some common functionality. For example, suppose I have the next type: template <typename A, typename B> struct Foo { Foo& func0() { /* common actions with A and B */; return *this; } void func1() { /* common actions with A and B */ } void func2() { /* common actions with A and B */ } } Then I want specialize it for one of the template parameters - for example, I want consider special case when B is int , and I want to

How to change behaviour of the method in groovy using that method in metaclass

点点圈 提交于 2020-01-02 04:48:29
问题 I would like to "spoil" plus method in Groovy in the following way: Integer.metaClass.plus {Integer n -> delegate + n + 1} assert 2+2 == 5 I am getting StackOverflowException (which is not surprising). Is there any way to use "original" plus method inside metaclass' closure? 回答1: The groovy idiomatic way is to save a reference to the old method and invoke it inside the new one. def oldPlus = Integer.metaClass.getMetaMethod("plus", [Integer] as Class[]) Integer.metaClass.plus = { Integer n ->

Dynamically adding class methods to a class

非 Y 不嫁゛ 提交于 2020-01-02 04:45:05
问题 I have the following snippet: FEED_TYPES = [ ('fan_mail', 'Fan Mail'), ('review', 'Review'), ('tip', 'Tip'), ('fan_user', 'Fan User'), ('fan_song', 'Fan Song'), ('fan_album', 'Fan Album'), ('played_song', 'Played Song'), ('played_album', 'Played Album'), ('played_radio', 'Played Radio'), ('new_event', 'New Event'), ] class Feed: @classmethod def do_create(cls, **kwargs): print kwargs @classmethod def create(cls, type, **kwargs): kwargs['feed_type'] = type cls.do_create(**kwargs) for type

Generate unique numbers at compile time

点点圈 提交于 2020-01-02 02:20:29
问题 I want to generate unique numbers for each class in my header, primes in my case primes but let's say this should only be consecutive numbers i.e. 1,2,3,4,etc. Of course I can hardcode these: struct A { enum { ID = 1; }; }; struct B { enum { ID = 2; }; }; struct C { enum { ID = 3; }; }; struct D { enum { ID = 4; }; }; This is very error-prone since in reality the classes are not that small and if I add a new class in the middle I have to change all the following numbers if I don't want to

When to use `method_missing`

非 Y 不嫁゛ 提交于 2020-01-02 02:20:12
问题 In the code below, method roar is not defined in class Lion , but still can be called using method_missing . class Lion def method_missing(name, *args) puts "Lion will #{name}: #{args[0]}" end end lion = Lion.new lion.roar("ROAR!!!") # => Lion will roar: ROAR!!! In which situations and how should I use this method_missing ? And is it safe to use? 回答1: It's entirely safe to use provided you use it in expected ways and don't get carried away. Not everything you can do is worth doing, after all.

C++11 way to write template for picking bigger integer type?

北慕城南 提交于 2020-01-02 01:25:51
问题 At compile time in C++11 in a template function that takes 2 template parameters, both of which must be unsigned integer types, I'd like to have a local variable have the type of whichever of the two template parameters has more bits. In C++03 I might write something like: template<bool, class T, class U> struct pick_first; template<class T, class U> struct pick_first<true, T, U> { typedef T type; }; template<class T, class U> struct pick_first<false, T, U> { typedef U type; }; template<class

C++11 way to write template for picking bigger integer type?

依然范特西╮ 提交于 2020-01-02 01:25:08
问题 At compile time in C++11 in a template function that takes 2 template parameters, both of which must be unsigned integer types, I'd like to have a local variable have the type of whichever of the two template parameters has more bits. In C++03 I might write something like: template<bool, class T, class U> struct pick_first; template<class T, class U> struct pick_first<true, T, U> { typedef T type; }; template<class T, class U> struct pick_first<false, T, U> { typedef U type; }; template<class

Compile time type tracing

╄→гoц情女王★ 提交于 2020-01-01 11:48:31
问题 Is it possible to add some magic construct around a Scala expression so that it prints the type during compilation? E.g. have some class, magic function, meta programming type, which does: val i = 1 Some(11).map(Trace(_ + 1)) // compile // prints: Int 回答1: Not exactly, but how 'bout this $ cat Test.scala def Trace[T] = identity[T] _ val i = 1 Some(11) map {x => Trace(x + 1)} $ scala -Xprint:typer Test.scala 2>&1 | egrep --o 'Trace\[.*\]' Trace[T >: Nothing <: Any] Trace[Int] The first Trace

setattr and getattr with methods

百般思念 提交于 2020-01-01 10:27:47
问题 I have a boiler platey class that delegates some actions to a reference class. It looks like this: class MyClass(): def __init__(self, someClass): self.refClass = someClass def action1(self): self.refClass.action1() def action2(self): self.refClass.action2() def action3(self): self.refClass.action3() This is the refClass: class RefClass(): def __init__(self): self.myClass = MyClass(self) def action1(self): #Stuff to execute action1 def action2(self): #Stuff to execute action2 def action3(self

Get Method Arguments using Ruby's TracePoint

主宰稳场 提交于 2020-01-01 10:12:12
问题 I'm able to get access to a Ruby method's arguments using the TracePoint API: def foo(foo_arg) end trace = TracePoint.trace(:call, :c_call) do |tp| tp.disable case tp.method_id when :foo, :sub method = eval("method(:#{tp.method_id})", tp.binding) method.parameters.each do |p| puts "#{p.last}: #{tp.binding.local_variable_get(p.last)}" end end tp.enable end trace.enable foo(10) # => foo_arg: 10 However when I try this with a c method call, I get an error. "foo".sub(/(f)/) { $1.upcase } script