metaprogramming

What does the tt metavariable type mean in Rust macros?

蹲街弑〆低调 提交于 2019-12-18 04:33:21
问题 I'm reading a book about Rust, and start playing with Rust macros. All metavariable types are explained there and have examples, except the last one – tt . According to the book, it is a “a single token tree”. I'm curious, what is it and what is it used for? Can you please provide an example? 回答1: That's a notion introduced to ensure that whatever is in a macro invocation correctly matches () , [] and {} pairs. tt will match any single token or any pair of parenthesis/brackets/braces with

Extract the return type of a function without calling it (using templates?)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-18 04:13:08
问题 I'm looking for a way in C++ to extract the return type of a function (without calling it). I presume this will require some template magic. float Foo(); int Bar(); magic_template<Foo>::type var1; // Here 'var1' should be of type 'float' magic_template<Bar>::type var2; // and 'var2' should be of type 'int' I am currently investigating how magic_template might be implemented, but have not found a solution so far. Any ideas? 回答1: Take a look at boost type traits library, in particular the

Obtaining const_iterator from iterator

六眼飞鱼酱① 提交于 2019-12-18 04:05:18
问题 Is there a metafunction f that maps an iterator to its corresponding const_iterator ? I.e. f<std::vector<T>::iterator>::type should yield std::vector<T>::const_iterator . 回答1: I am not aware of such a metafunction. Not all iterators have a corresponding const_iterator. E.g. insert_iterator. So such a metafunction would need to decide what it is going to do in such cases. 回答2: I can think of something for a reverse_iterator : using the base member function the decltype , one could extract the

How to get attributes that were defined through attr_reader or attr_accessor

女生的网名这么多〃 提交于 2019-12-18 03:05:16
问题 Suppose I have a class A class A attr_accessor :x, :y def initialize(x,y) @x, @y = x, y end end How can I get x and y attributes without knowing how exactly they were named. E.g. a = A.new(5,10) a.attributes # => [5, 10] 回答1: Use introspection, Luke! class A attr_accessor :x, :y def initialize(*args) @x, @y = args end def attrs instance_variables.map{|ivar| instance_variable_get ivar} end end a = A.new(5,10) a.x # => 5 a.y # => 10 a.attrs # => [5, 10] 回答2: While Sergio's answer helps, it will

Ruby 2.0 How do I uninclude a module out from a module after including it?

↘锁芯ラ 提交于 2019-12-18 02:46:15
问题 module X end module Y end module Z #TODO include X replacement of including Y #TODO include Y replacement of including X end Is there a way to work around the fact that ruby contains no uninclude keyword?? 回答1: If you really need this kind of functionality, you could probably do it by using refinements. class Foo end module X def x puts 'x' end end module Y end module R refine Foo do include X include Y end end # In a separate file or class using R # Foo now includes X and Y Foo.new.x # In a

Ruby 2.0 How do I uninclude a module out from a module after including it?

心不动则不痛 提交于 2019-12-18 02:46:06
问题 module X end module Y end module Z #TODO include X replacement of including Y #TODO include Y replacement of including X end Is there a way to work around the fact that ruby contains no uninclude keyword?? 回答1: If you really need this kind of functionality, you could probably do it by using refinements. class Foo end module X def x puts 'x' end end module Y end module R refine Foo do include X include Y end end # In a separate file or class using R # Foo now includes X and Y Foo.new.x # In a

Javascript automatic getter/setters (John Resig Book)

与世无争的帅哥 提交于 2019-12-17 23:23:24
问题 I'm reading "Pro Javascript Techniques" from John Resig, and I'm confused with an example. This is the code: // Create a new user object that accepts an object of properties function User( properties ) { // Iterate through the properties of the object, and make sure // that it's properly scoped (as discussed previously) for ( var i in properties ) { (function(){ // Create a new getter for the property this[ "get" + i ] = function() { return properties[i]; }; // Create a new setter for the

Encrypting / obfuscating a string literal at compile-time

别说谁变了你拦得住时间么 提交于 2019-12-17 22:51:27
问题 I want to encrypt/encode a string at compile time so that the original string does not appear in the compiled executable. I've seen several examples but they can't take a string literal as argument. See the following example: template<char c> struct add_three { enum { value = c+3 }; }; template <char... Chars> struct EncryptCharsA { static const char value[sizeof...(Chars) + 1]; }; template<char... Chars> char const EncryptCharsA<Chars...>::value[sizeof...(Chars) + 1] = { add_three<Chars>:

Find classes available in a Module

非 Y 不嫁゛ 提交于 2019-12-17 21:57:53
问题 I have a module MyModule . I dynamically load classes into it. How can I get a list of the classes defined within its namespace? Example: def load_plugins Dir.glob(File.dirname(__FILE__) + '/plugins/*.rb') do |f| MyModule.class_eval File.read(f) end # now how can I find the new classes I've loaded into MyModule? end I should say that each f contains something like "class Foo; end". You can also think of it like this: in Rails, how could I programatically find all classes defined within the

Getting template metaprogramming compile-time constants at runtime

白昼怎懂夜的黑 提交于 2019-12-17 21:44:47
问题 Background Consider the following: template <unsigned N> struct Fibonacci { enum { value = Fibonacci<N-1>::value + Fibonacci<N-2>::value }; }; template <> struct Fibonacci<1> { enum { value = 1 }; }; template <> struct Fibonacci<0> { enum { value = 0 }; }; This is a common example and we can get the value of a Fibonacci number as a compile-time constant: int main(void) { std::cout << "Fibonacci(15) = "; std::cout << Fibonacci<15>::value; std::cout << std::endl; } But you obviously cannot get