metaprogramming

C++11 is_same type trait for templates

巧了我就是萌 提交于 2019-12-18 10:23:20
问题 Is it possible to check that type T is an std::array of arbitrary type and size? I can check for a particular array, for instance: is_same<T, std::array<int,5>>::value But I'd like to check that T is any instantiation of std::array . Something like below (which, of course, does not compile): is_same<T, std::array>::value Is there a way to achieve this (maybe not using is_same )? 回答1: You have to write your own, but it's simple: template<typename> struct is_std_array : std::false_type {};

How can I deduce the outer type of an inner type in C++?

陌路散爱 提交于 2019-12-18 08:48:28
问题 I have many classes exposing an inner type named Binding . For instance, one of them could be: struct Message { struct Binding { }; }; I invoke a function apply like this: apply< Message >([](Message::Binding& x) { // setup binding fields }); for I wrote template <class TMessage, class TBindingExpression> void apply(const TBindingExpression& expr) { typedef typename TMessage::Binding BindingType; BindingType binding; expr(binding); apply(MessageUtil::typeId< TMessage >(), binding); } Since

How to convert any method to infix operator in ruby

天涯浪子 提交于 2019-12-18 07:12:05
问题 In some language such as Haskell, it is possible to use any function taking two arguments as an infix operator. I find this notation interesting and would like to achieve the same in ruby. Given a imaginary method or_if_familiar I'd like to be able to write something like "omg" or_if_familiar "oh!" instead of or_if_familiar("omg", "oh!") How one would create such a notation in ruby (without modifying ruby itself)? 回答1: A bit late to the party but I've been toying around with it and you can

Is it possible to run code after each line in Ruby?

帅比萌擦擦* 提交于 2019-12-18 06:49:54
问题 I understand that it is possible to decorate methods with before and after hooks in ruby, but is it possible to do it for each line of a given method? For example, I have an automation test and I want to verify that after each step there no error shown on the page. The error is shown as a red div and is not visible to Ruby as raise or anything like that, so I have to check for it manually (there are several other use cases as well). I understand that it might be possible using set_trace_func

Is it possible to run code after each line in Ruby?

家住魔仙堡 提交于 2019-12-18 06:49:32
问题 I understand that it is possible to decorate methods with before and after hooks in ruby, but is it possible to do it for each line of a given method? For example, I have an automation test and I want to verify that after each step there no error shown on the page. The error is shown as a red div and is not visible to Ruby as raise or anything like that, so I have to check for it manually (there are several other use cases as well). I understand that it might be possible using set_trace_func

How do I undo meta class changes after executing GroovyShell?

橙三吉。 提交于 2019-12-18 05:50:13
问题 For example, if I execute a Groovy script, which modifies the String meta class, adding a method foo() GroovyShell shell1 = new GroovyShell(); shell1.evaluate("String.metaClass.foo = {-> delegate.toUpperCase()}"); when I create a new shell after that and execute it, the changes are still there GroovyShell shell2 = new GroovyShell(); Object result = shell2.evaluate("'a'.foo()"); Is there a way to undo all meta class changes after executing the GroovyShell? I tried shell1.getClassLoader()

Why is Python's eval() rejecting this multiline string, and how can I fix it?

删除回忆录丶 提交于 2019-12-18 05:44:22
问题 I am attempting to eval the following tab-indented string: '''for index in range(10): os.system("echo " + str(index) + "") ''' I get, "There was an error: invalid syntax , line 1" What is it complaining about? Do I need to indent to match the eval() statement, or write it to a string file or temp file and execute that, or something else? Thanks, 回答1: eval evaluates stuff like 5+3 exec executes stuff like for ... >>> eval("for x in range(3):print x") Traceback (most recent call last): File "

Ruby.Metaprogramming. class_eval

烂漫一生 提交于 2019-12-18 05:26:06
问题 There seem to be a mistake in my code. However I just can't find it out. class Class def attr_accessor_with_history(attr_name) attr_name = attr_name.to_s attr_reader attr_name attr_writer attr_name attr_reader attr_name + "_history" class_eval %Q{ @#{attr_name}_history=[1,2,3] } end end class Foo attr_accessor_with_history :bar end f = Foo.new f.bar = 1 f.bar = 2 puts f.bar_history.to_s I would expect it to return an array [1,2,3] . However, it doesn't return anything. 回答1: You will find a

Function to output function name

孤人 提交于 2019-12-18 04:53:14
问题 Is it possible in Haskell to implement a function which returns its own function name? A possible type could be (a -> b) -> String . 回答1: You want a function that takes a function argument, and returns the definition site variable name that corresponds to the name of that function? This isn't possibly without meta-programming, which is usually a sign you're doing something wrong :). But assuming you're not, one way to achieve something in the right direction is via Template Haskell, which can

Can the type of a base class be obtained from a template type automatically?

左心房为你撑大大i 提交于 2019-12-18 04:46:22
问题 I am trying to use template meta-programming to determine the base class. Is there a way to get the base class automatically without explicitly specializing for each derived class? class foo { public: char * Name() { return "foo"; }; }; class bar : public foo { public: char * Name() { return "bar"; }; }; template< typename T > struct ClassInfo { typedef T Base; }; template<> struct ClassInfo<bar> { typedef foo Base; }; int main() { ClassInfo<foo>::Base A; ClassInfo<bar>::Base B; std::cout <<