metatype

Can I get the Owning Object of a Member Function Template Parameter?

喜欢而已 提交于 2019-12-18 19:09:16
问题 Given a object: struct foo { void func(); }; Now given the templatized function declaration: template<typename T, T F> void bar(); So bar will be taking in a member function like so: bar<decltype(&foo::func), &foo::func>() In the body of bar I want to recover the type foo from T . Can I do that? I want to be able to do something like this: get_obj<T> myfoo; (myfoo.*F)(); I know that get_obj isn't a thing, but would there be a way to write it? 回答1: template<class T> struct get_memfun_class;

Swift 3, is the “.self” in a metatype issue actually correct?

三世轮回 提交于 2019-12-02 01:15:53
问题 I have an extension to walk up the view controller chain (even through container views, which is very handy) public extension UIViewController // go up to a certain class { public func above<T>(_ : T.Type)->(T) { var p:UIResponder = self repeat { p = p.next! } while !(p is T) return p as! T } } (Aside, NB, Swift3 needs the "!" on p.next: unfortunately I'm not sure exactly why.) So, say you have a view controller class "General", you can self.above(General).clickedHamburgerMenuButton() and it

Check whether Swift object is an instance of a given metatype

浪尽此生 提交于 2019-11-28 13:37:20
I need to keep a collection of Swift metatypes and write a function which will check if a given object is an instance of one of them. I can do that easily in Java: Class c = x.getClass(); c.isInstance(someObj) However, I have no idea how to do that in Swift: var isInt = 7 is Int.Type // compiles let x = Int.self var isInt = 7 is x // compiler error - Use of undeclared type 'x' Is this even possible to be done in Swift? Unfortunately, you can currently only use a named type with the is operator, you cannot yet use an arbitrary metatype value with it (although really IMO you ought to be able to)

Check whether Swift object is an instance of a given metatype

回眸只為那壹抹淺笑 提交于 2019-11-27 07:44:33
问题 I need to keep a collection of Swift metatypes and write a function which will check if a given object is an instance of one of them. I can do that easily in Java: Class c = x.getClass(); c.isInstance(someObj) However, I have no idea how to do that in Swift: var isInt = 7 is Int.Type // compiles let x = Int.self var isInt = 7 is x // compiler error - Use of undeclared type 'x' Is this even possible to be done in Swift? 回答1: Unfortunately, you can currently only use a named type with the is