metaprogramming

Grails - Making Methods Globally Available and Metaclass Programming

和自甴很熟 提交于 2020-01-15 08:15:42
问题 I inserted this line into my init() of my BootStrap class Integer.metaClass.minutes = { 60000L * delegate } I was then not able to use it from a Job class (Quartz plugin). Do I put this line of code somewhere else to make it a global modification? I was also wondering the best way to make a function available inside all classes in Grails. Like a global function. Would it be to extend the Object metaclass? or is there a better way? 回答1: Do I put this line of code somewhere else to make it a

SFINAE to check for inherited member functions

二次信任 提交于 2020-01-15 05:51:10
问题 Using SFINAE, i can detect wether a given class has a certain member function. But what if i want to test for inherited member functions? The following does not work in VC8 and GCC4 (i.e. detects that A has a member function foo() , but not that B inherits one): #include <iostream> template<typename T, typename Sig> struct has_foo { template <typename U, U> struct type_check; template <typename V> static char (& chk(type_check<Sig, &V::foo>*))[1]; template <typename > static char (& chk(...))

Forbid code to compile if some function is not called

£可爱£侵袭症+ 提交于 2020-01-15 03:55:13
问题 Is there a way in C++ to forbid code to compile if the specific function was not called. Imagine I have some class: class CExample { public: void Init(); void DoWork(); }; Is there a way to forbid calling DoWork() if the Init() function was not called for class object? I want to forbid writing such a code: CExample e; e.DoWork(); and permit this version: CExample e; e.Init(); e.DoWork(); Can I reach this behaviour somehow with metaprogramming? 回答1: You can just use a constructor instead of

Square root metafunction?

风流意气都作罢 提交于 2020-01-14 08:54:52
问题 Is it possible to compute the square root of an integer with a metafunction with the following signature : template<unsigned int N> inline double sqrt(); (or maybe using the constexpr keyword, I don't know what is the best). With that, sqrt<2>() would be replaced by 1.414... at compile-time. What would be the best implementation for a such function ? 回答1: This may not be what you are looking for, but I wanted to make sure you realized that typically with optimization the compiler will

C++ template non-type parameter type deduction

自闭症网瘾萝莉.ら 提交于 2020-01-14 07:38:27
问题 I'am trying to do this work: template < typename T, T VALUE > void f() { /* ... */ } int main() { f<10>(); // implicit deduction of [ T = int ] ?? return (0); } The purpose is to simplify a much more complex template. After many searches, I don't find any way to do that on C++0x, so stackoverflow is my last resort. without specify all type of T possible... I am on g++ C++0x, so sexy stuff is allowed. 回答1: C++0x introduces decltype() , which does exactly what you want. int main() { f<decltype

How to check if class has pointers in C++14

孤人 提交于 2020-01-14 02:00:02
问题 I've got the classes: struct A { // has no pointer members, POD - it's fine int a, b; char c; }; struct B { // has no pointer members, but not POD - it's still fine int a, b; std::string s; }; struct C { // has pointer members, it's not fine int a,b; char* cs; }; I need to detect in compile time if any class has the properties of struct C , i.e. has pointers as members. Short reasoning: I need to assure a user-defined type can be safely serialized and deserialized to some buffer by copying or

Heterogenous container using only static polymorphism

这一生的挚爱 提交于 2020-01-13 09:39:05
问题 My goal is to implement a container (here a set of stacks, one for each type) that accepts many different types of objects simultaneously. This would be trivial to do at runtime, using void pointers (or a common base class for all stored types) and runtime type indentification (RTTI). Since all types the container is going to hold are known at compile time, it may (or may not) be possible to make such a class using templates. I am aware that boost::variant already provides similar

Meta programming: Declare a new struct on the fly

喜你入骨 提交于 2020-01-13 07:48:08
问题 Is it possible to declare a new type (an empty struct , or a struct without an implementation) on the fly? E.g. constexpr auto make_new_type() -> ???; using A = decltype(make_new_type()); using B = decltype(make_new_type()); using C = decltype(make_new_type()); static_assert(!std::is_same<A, B>::value, ""); static_assert(!std::is_same<B, C>::value, ""); static_assert(!std::is_same<A, C>::value, ""); A "manual" solution is template <class> struct Tag; using A = Tag<struct TagA>; using B = Tag

How can classes be made parametric in Perl 6?

旧街凉风 提交于 2020-01-12 15:46:05
问题 Normally in Perl 6, only roles are allowed to be parametric. Here, we'll be attempting to make classes, a kind (referred to from here on out as a metaobject) that isn't normally allowed to be parametric, parametric. If you try to make a class parametric the naive way, this happens: bastille% perl6 -e 'class Foo[::T] {}' ===SORRY!=== Error while compiling -e Unable to parse class definition at -e:1 ------> class Foo⏏[::T] {} expecting any of: generic role But if you take a look at what

list Rails controller instance variables

此生再无相见时 提交于 2020-01-12 14:17:29
问题 i was trying to list the instance variables inside a controller but came up with irb>HomeController.instance_variable_names => ["@visible_actions", "@inheritable_attributes", "@controller_path", "@action_methods", "@_process_action_callbacks"] and I tried it on the action irb>HomeController.action("index").instance_variable_names => [] so what do controller Instance variables belong to? 回答1: The instance variables belong to the instantiated controller object, and are only created when the