metaprogramming

Python __new__ metaclass behavior with inheritance

 ̄綄美尐妖づ 提交于 2020-01-06 08:16:14
问题 I have two questions regarding the behavior of running the below code. Why is __new__ even being called without instantiating an object? I thought __new__ controlled the creation of a new instance . Next, why does delattr raise an AttributeError when hasattr returns True for size ? class ShapeBase(type): def __new__(cls, name, bases, attrs): rv = super(ShapeBase, cls).__new__(cls, name, bases, attrs) parents = [base for base in bases if isinstance(base, ShapeBase)] # don't do anything unless

Binding a pretty-printer to boost::phoenix actors when iterating with boost::fusion

时间秒杀一切 提交于 2020-01-06 04:09:27
问题 This question is a follow-up to Pointers to class members when iterating with boost::fusion, where the accepted solution works. Now, I want not only to add the (primitive) values to the property-map, but use a pretty-printer to improve how the values are displayed. This mechanism will also be used in case the values are not trivial to print. So, there is some pretty-printer like this: template<typename T> std::string prettyPrinter(const T& t); template<> std::string prettyPrinter(const std:

Simulate the switch comportment in template specialization

冷暖自知 提交于 2020-01-06 03:59:28
问题 Here is the following code that shows a design I use. I made a wrapper class that encapsulates a template class. A method of the wrapper permits with a switch to choose the specialization I want : #include <memory> #include <string> /* Interface */ struct IFoo { virtual void lol(void) = 0; }; /* Template Specialization */ template<std::size_t N> class Foo : public IFoo { void lol(void) {} }; /* Wrapper for the template */ class FooWrapper : public IFoo { std::unique_ptr<IFoo> mfoo; public:

Is There a Good Pattern for Creating a Unique Id based on a Type?

試著忘記壹切 提交于 2020-01-06 03:13:07
问题 I have a template that creates a unique identifier for each type it is instanced. Here's a streamlined version of the template: template <typename T> class arType { static const arType Id; // this will be unique for every instantiation of arType<>. } // Address of Id is used for identification. #define PA_TYPE_TAG(T) (&arType<T >::Id) This works when you have an executable made purely of static libraries. Unfortunately we're moving to an executable made up of dlls. Each dlls could potentially

Ruby: const_set outside block?

ε祈祈猫儿з 提交于 2020-01-04 14:20:58
问题 I want to mock a class with Ruby. How do I write a method that will take care of the boilerplate code? The following code: module Mailgun end module Acani def self.mock_mailgun(mock) temp = Mailgun const_set(:Mailgun, mock) p Mailgun yield ensure const_set(:Mailgun, temp) end end Acani.mock_mailgun('mock') { p Mailgun } prints: "mock" Mailgun What's going on here? Why is Mailgun its original value inside the block? Does this have to do with Ruby bindings? Ruby version: 2.1.1p76 回答1: Try

metaprogramming access local variables

▼魔方 西西 提交于 2020-01-04 13:37:20
问题 class Foo def initialize bar = 10 end fiz = 5 end Is there a possibility to get these local values (outside the class) ? 回答1: The local variable in initialize would be lost. You are able to get the value fiz outside of the class, but only upon defining that class, and recording the return of the definition of the class. return_of_class_definition = (class A ; fiz = 5 ; end) would assign the value of fiz to the variable. You can also use binding but of course, this means changing the class,

metaprogramming access local variables

天涯浪子 提交于 2020-01-04 13:37:11
问题 class Foo def initialize bar = 10 end fiz = 5 end Is there a possibility to get these local values (outside the class) ? 回答1: The local variable in initialize would be lost. You are able to get the value fiz outside of the class, but only upon defining that class, and recording the return of the definition of the class. return_of_class_definition = (class A ; fiz = 5 ; end) would assign the value of fiz to the variable. You can also use binding but of course, this means changing the class,

Create member alias based on a template parameter condition C++17

£可爱£侵袭症+ 提交于 2020-01-04 13:25:46
问题 So, I am trying to simplify the use of my generic classes and came across the following idea: The following struct is given: template <size_t size> struct Vector { std::array<float, size> data; float& x = data[0]; float& y = data[1]; // Declare y only if size is > 0 float& z = data[2]; // Declare z only if size is > 1 float& w = data[3]; // Declare w only if size is > 2 }; Obviously, if I try to run the program like this, the array will throw an out of range exception. Now is there a way to

Pointers to class members when iterating with boost::fusion

点点圈 提交于 2020-01-04 06:17:50
问题 I have a boost::graph that uses bundled properties like the following: struct Vertex { std::string id; }; If I want to use this information in boost::dynamic_properties (e.g. for printing in graphml-format), I can use something like that: template<typename T> std::string myPrettyPrinter(const T& t); int main() { using namespace boost; MyGraph g; dynamic_properties dp; dp.property("id", make_transform_value_property_map( & myPrettyPrinter<std::string>, get(&Vertex::id, g) ) ); } Since the

Including an invariant assumption in a template function

点点圈 提交于 2020-01-04 04:23:26
问题 Consider a typical finite difference application: // assuming T_size > 2 void process_T(double *T0, double *T, const int &T_size, bool periodic) { for (int i = 0; i < T_size; ++i) { double sum = 0; double base = T0[i]; if (i > 0) sum += (T0[i-1]-base); if (i < 0) sum += (T0[i+1]-base); if (periodic) { if (i == 0) sum += (T0[T_size-1]-base); if (i == T_size-1) sum += (T0[0]-base); } else { if (i == 1 || i == T_size-1) sum += 0.5*(T0[i-1]-base); if (i == 0 || i == T_size-2) sum += 0.5*(T0[i+1]