metaprogramming

Is it possible to transform the types in a parameter pack?

本小妞迷上赌 提交于 2019-12-30 03:58:23
问题 Is it possible to transform the types of a parameter pack and pass it on? E.g. given the following: template<class... Args> struct X {}; template<class T> struct make_pointer { typedef T* type; }; template<class T> struct make_pointer<T*> { typedef T* type; }; Can we define a template magic or something similar so that the following assertion holds: typedef magic<X, make_pointer, int, char>::type A; typedef X<int*, char*> B; static_assert(is_same<A, B>::value, ":("); 回答1: Yes we can do that

What’s the best way to discover all variables a Perl application has currently defined?

一个人想着一个人 提交于 2019-12-30 02:12:14
问题 I am looking for best, easiest way to do something like: $var1="value"; bunch of code..... **print allVariablesAndTheirValuesCurrentlyDefined;** 回答1: Package variables? Lexical variables? Package variables can be looked up via the symbol table. Try Devel::Symdump: #!/path/to/perl use Devel::Symdump; package example; $var = "value"; @var = ("value1", "value2"); %var = ("key1" => "value1", "key2" => "value2"); my $obj = Devel::Symdump->new('example'); print $obj->as_string(); Lexical variables

Generic programming vs. Metaprogramming

别等时光非礼了梦想. 提交于 2019-12-29 14:29:21
问题 What exactly is the difference? It seems like the terms can be used somewhat interchangeably, but reading the wikipedia entry for Objective-c, I came across: In addition to C’s style of procedural programming, C++ directly supports certain forms of object-oriented programming, generic programming, and metaprogramming. in reference to C++. So apparently they're different? 回答1: Programming : Writing a program that creates, transforms, filters, aggregates and otherwise manipulates data.

Generic programming vs. Metaprogramming

痞子三分冷 提交于 2019-12-29 14:29:08
问题 What exactly is the difference? It seems like the terms can be used somewhat interchangeably, but reading the wikipedia entry for Objective-c, I came across: In addition to C’s style of procedural programming, C++ directly supports certain forms of object-oriented programming, generic programming, and metaprogramming. in reference to C++. So apparently they're different? 回答1: Programming : Writing a program that creates, transforms, filters, aggregates and otherwise manipulates data.

How do you evaluate a string as a clojure expression?

房东的猫 提交于 2019-12-28 16:30:43
问题 How would I get something similar to the following?: (evaluate-text "(+ 1 2)") ; resolves to 3 回答1: (load-string "(+ 1 2)") 回答2: user> (eval (read-string "(+ 1 2)")) 3 You probably shouldn't ever need to do this. Macros and fns make this kind of thing unnecessary 99% of the time. This is quite brittle, and can be unsafe if these strings are coming from user input, and so on. 回答3: How similar does it have to be? Clojure's eval works on lists, so: (eval (list + 1 2)) #=> 3 来源: https:/

SFINAE + sizeof = detect if expression compiles

蹲街弑〆低调 提交于 2019-12-28 12:09:48
问题 I just found out how to check if operator<< is provided for a type. template<class T> T& lvalue_of_type(); template<class T> T rvalue_of_type(); template<class T> struct is_printable { template<class U> static char test(char(*)[sizeof( lvalue_of_type<std::ostream>() << rvalue_of_type<U>() )]); template<class U> static long test(...); enum { value = 1 == sizeof test<T>(0) }; typedef boost::integral_constant<bool, value> type; }; Is this trick well-known, or have I just won the metaprogramming

Use templates to get an array's size and end address

一笑奈何 提交于 2019-12-28 07:06:09
问题 You can use templates to find the length of an array. template<typename T, size_t N> size_t arraylen( T(&)[N] ) { return N; } I'd like to take this idea one step further. struct Foo { template< typename T, size_t N > Foo( /* ??? */ ) : ptr(?), size(?) { } char* ptr; size_t size; }; int main() { Foo foo("test"); const char bar[] = "test2"; Foo foo2(bar); const char* baz = bar; Foo foo3(baz); // compiler error. } However, for the life of me I can't get the syntax to compile. I think part of

ROR : Workflow gem : I want to implement dynamic states from database

烂漫一生 提交于 2019-12-25 15:01:05
问题 I am currrently working on a project that has to implement dynamic workflow. Dynamic : I store workflow's states in database table called wf_steps and the workflow gem has to create states for a particular workflow from the database For that I am trying to use the workflow gem. You can see how it initializes states and corresponding events in the gem's github-page. My code: class SpsWorkflow < ActiveRecord::Base belongs_to :user has_many :wf_steps include Workflow workflow do # binding.pry #

Conditionally calling property/methods on classes

走远了吗. 提交于 2019-12-25 10:55:55
问题 I have a custom Array type class in Typescript. I didn't extend it because of the difficulties that comes with that, so I just use a private array which I have to directly reference each time I want to use an array method. export class ObjectCollection<T extends ICollection> { public _Collection: T; public get Collection(): T { return this._Collection; } public getInstance: (id: string) => IListItem; constructor(collection: T) { this._Collection = collection; // used for getting instance from

Use def_method inside ActiveRecord model

拥有回忆 提交于 2019-12-25 10:55:35
问题 So I have AR model like the following, and I want to dynamically generate a few instance methods like #fallbackable_header_script, #fallbackable_header_content... etc, just like the #fallbackable_background I've already written. What's the best way to do this? class Course < ActiveRecord::Base FALLBACKABLE_ATTRIBUTES = :header_script, :header_content, :footer_content OTHER_FALLBACKABLE_ATTRIBUTES = :css_config def fallbackable_background read_attribute(:background) ? background : self.user