Tag: 元编程

在部分特定的类上标记调度和静态方法

假设我想写一个generics函数void f<T>() ,如果T是一个PODtypes,那么做一件事,如果T是非POD(或任何其他任意谓词),则做另一件事。 实现这一点的一种方法是使用像标准库一样的标签分派模式来处理迭代器类别: template <bool> struct podness {}; typedef podness<true> pod_tag; typedef podness<false> non_pod_tag; template <typename T> void f2(T, pod_tag) { /* POD */ } template <typename T> void f2(T, non_pod_tag) { /* non-POD */ } template <typename T> void f(T x) { // Dispatch to f2 based on tag. f2(x, podness<std::is_pod<T>::value>()); } 另一种方法是使用部分专用types的静态成员函数: […]

Javascript有类似Ruby的method_missingfunction吗?

在Ruby中,我认为你可以调用一个尚未定义的方法,并捕获调用方法的名称,并在运行时处理这个方法。 Javascript可以做同样的事情吗?

Rubystring类名称

我想创build一个新的类,将从ActiveRecord :: Baseinheritance类需要dynamic生成一个string "general_systems".camelize.singularize = Class.new < ActiveRecord::Base 但是我不断收到错误: undefined method `singularize=' for "GeneralSystems":String 我也试着“串化”string >> foo = "general_systems".camelize.singularize => "GeneralSystem" >> foo.constantize NameError: uninitialized constant GeneralSystem from /var/lib/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/inflector/methods.rb:124:in `block in constantize' from /var/lib/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/inflector/methods.rb:123:in `each' from /var/lib/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/inflector/methods.rb:123:in `constantize' from /var/lib/gems/1.9.1/gems/activesupport-3.0.5/lib/active_support/core_ext/string/inflections.rb:43:in `constantize' from (irb):4 from /usr/bin/irb:12:in `<main>' >> foo.constantize = Class.new NoMethodError: undefined method `constantize=' for […]

在Ruby中method_missing陷阱

在Ruby中定义method_missing方法时有什么要注意的吗? 我想知道是否有一些从inheritance,exception抛出,性能或其他任何不太明显的交互。

C ++是否支持编译时计数器?

为了内省的目的,有时候我想自动分配序列号给types,或类似的东西。 不幸的是,模板元编程基本上是一种function性语言,因此缺乏全局variables或可修改的状态来实现这样的计数器。 还是呢? 请求示例代码: #include <iostream> int const a = counter_read; counter_inc; counter_inc; counter_inc; counter_inc; counter_inc; int const b = counter_read; int main() { std::cout << a << ' ' << b << '\n'; // print "0 5" counter_inc_t(); counter_inc_t(); counter_inc_t(); std::cout << counter_read << '\n'; // print "8" struct { counter_inc_t d1; char x[ […]

定义交换操作时减less代码重复

我有一套名为overlap的可交换二元函数的重载,它接受两种不同的types: class A a; class B b; bool overlap(A, B); bool overlap(B, A); 当且仅当一个形状与另一个形状重叠时,我的函数overlap返回true – 这是讨论多方法时使用的一个常见示例。 因为overlap(a, b)等价于overlap(b, a) ,所以我只需要实现关系的一个“边”。 一个重复的解决scheme是写这样的东西: bool overlap(A a, B b) { /* check for overlap */ } bool overlap(B b, A a) { return overlap(a, b); } 但我宁愿不写一个额外的N! / 2 N! / 2相同function的简单版本,通过使用模板来生成它们。 template <typename T, typename U> bool […]

模板元编程 – 使用Enum Hack和Static Const之间的区别

我想知道使用模板元编程技术时,使用静态常量和枚举黑客之间有什么区别。 EX:(通过TMP的斐波纳契) template< int n > struct TMPFib { static const int val = TMPFib< n-1 >::val + TMPFib< n-2 >::val; }; template<> struct TMPFib< 1 > { static const int val = 1; }; template<> struct TMPFib< 0 > { static const int val = 0; }; 与 template< int n > struct TMPFib […]

java自定义注释:使一个属性可选

我定义了我自己的自定义注释 @Target(value={ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface MyCustomAnnotation { Class<?> myType(); } 如果可以的话,我可以如何使属性可选

Python装饰器使function忘记它属于一个类

我想写一个装饰器来做日志logging: def logger(myFunc): def new(*args, **keyargs): print 'Entering %s.%s' % (myFunc.im_class.__name__, myFunc.__name__) return myFunc(*args, **keyargs) return new class C(object): @logger def f(): pass C().f() 我想这打印: Entering Cf 但是我却得到这个错误信息: AttributeError: 'function' object has no attribute 'im_class' 大概这是与'logging器'内'myFunc'的范围有关,但我不知道是什么。

查找模块中可用的类

我有一个模块MyModule 。 我dynamic加载类到它。 我如何获得在其名称空间中定义的类的列表? 例: 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 我应该说,每个f包含像“Foo类;结束”的东西。 你也可以这样想:在Rails中,我怎样才能以编程方式find在ActiveRecord模块中定义的所有类?