metaprogramming

What does T::* mean in template's parameters?

天大地大妈咪最大 提交于 2019-12-19 11:40:55
问题 Following the article written in here: I came across this code (shortened and changed for clarity): template <class T> struct hasSerialize { // This helper struct permits us to check that serialize is truly a method. // The second argument must be of the type of the first. // For instance reallyHas<int, 10> would be substituted by reallyHas<int, int 10> and works! // reallyHas<int, &C::serialize> would be substituted by reallyHas<int, int &C::serialize> and fail! // Note: It only works with

How to make template parameter

一个人想着一个人 提交于 2019-12-19 10:54:36
问题 How do I create a metafunction that takes any kind of function pointer? In the code below, how do I get rid of "decltype(&f)" ? template <class FuncType, FuncType functionPointer> void runFunc() { functionPointer(); } runFunc<decltype(&f),f>(); I don't want to have to specify the type of f seperately; the information is already there in f. I'd prefer not to resort to defines to solve this. This is basically the templated function type idiom applied to meta-programming; I don't want to know

Ruby: automatically wrapping methods in event triggers

China☆狼群 提交于 2019-12-19 10:17:37
问题 Heres what I have/want: module Observable def observers; @observers; end def trigger(event, *args) good = true return good unless (@observers ||= {})[event] @obersvers[event].each { |e| good = false and break unless e.call(self, args) } good end def on(event, &block) @obersvers ||= {} @obersvers[event] ||= [] @observers[event] << block end end class Item < Thing include Observable def pickup(pickuper) return unless trigger(:before_pick_up, pickuper) pickuper.add_to_pocket self trigger(:after

Transform tuple type

自作多情 提交于 2019-12-19 10:15:20
问题 So I'm new to boost MPL, and I don't know how to use it with standard types. I want a metafunction that coverts this type: std::tuple<T0, T1, ..., TN> Into this: std::tuple< std::function<T0(std::tuple<T0, T1, ...>, std::tuple<T0, T1, ...>)>, std::function<T1(std::tuple<T0, T1, ...>, std::tuple<T0, T1, ...>)>, ..., std::function<TN(...)> > and it seems like this could be done with transform, but I want to have a tuple-type, not a vector of types. (It doesn't have to use MPL actually, but I

Transforming mpl vector with own function

你说的曾经没有我的故事 提交于 2019-12-19 09:09:28
问题 I want to multiply each element in an mpl::vector by an int . First, a metafunction to multiply an int_ with an int . template <int i> struct multiply_scalar { template<typename T> struct apply { typedef int_<(T::value * i)> type; }; }; Here are the calls I want to make. typedef vector<int_<3>, int_<4> > my_vec; typedef typename transform< my_vec, multiply_scalar<2> >::type my_vec_2; typedef vector<int_<6>, int_<8> > my_vec_3; BOOST_MPL_ASSERT(( boost::is_same< my_vec_2, my_vec_3 > )); /

How work pre-defined descriptors in functions?

老子叫甜甜 提交于 2019-12-19 07:47:23
问题 Python functions have a descriptors. I believe that in most cases I shouldn't use this directly but I want to know how works this feature? I tried a couple of manipulations with such an objects: def a(): return 'x' a.__get__.__doc__ 'descr.__get__(obj[, type]) -> value' What is the obj and what is the type? >>> a.__get__() TypeError: expected at least 1 arguments, got 0 >>> a.__get__('s') <bound method ?.a of 's'> >>> a.__get__('s')() TypeError: a() takes no arguments (1 given) Sure that I

How do I add a method to the table type?

瘦欲@ 提交于 2019-12-19 02:28:08
问题 How do I add a method to the table type? I'm trying to write a method that searches through the values of a table. So far I have. function table:contains(value) for _, v in ipairs(self) do if v == value then return true end end return false end Yet when I try to do the following. t = {'four', 'five', 'six'} t:contains('five') I get the error. stdin:1: attempt to call method 'contains' (a nil value) Any suggestions? 回答1: There is no single metatable for all tables . Unlike strings and numbers,

Programmatically derive a regular expression from a string

倾然丶 夕夏残阳落幕 提交于 2019-12-18 17:09:02
问题 I would like to input a string and return a regular expression that can be used to describe the string's structure. The regex will be used to find more strings of the same structure as the first. This is intentionally ambiguous because I will certainly miss a case that someone in the SO community will catch. Please post any and all possible ways to do this. 回答1: The trivial answer, and probably not what you want, is: return the input string (with regex special characters escaped). That is

Programmatically derive a regular expression from a string

你说的曾经没有我的故事 提交于 2019-12-18 17:07:11
问题 I would like to input a string and return a regular expression that can be used to describe the string's structure. The regex will be used to find more strings of the same structure as the first. This is intentionally ambiguous because I will certainly miss a case that someone in the SO community will catch. Please post any and all possible ways to do this. 回答1: The trivial answer, and probably not what you want, is: return the input string (with regex special characters escaped). That is

Using SFINAE to check if the type is complete or not [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-18 14:35:06
问题 This question already has answers here : How to write `is_complete` template? (7 answers) Closed 5 years ago . Is it possible to check with SFINAE if the type is completely defined? E.g. template <class T> struct hash; template <> struct hash<int> {}; // is_defined_hash_type definition... enum Enum { A, B, C, D }; static_assert ( is_defined_hash_type<int> ::value, "hash<int> should be defined"); static_assert (! is_defined_hash_type<Enum>::value, "hash<Enum> should not be defined"); The