metaprogramming

Automatic counter for derived class / Alternative?

爷,独闯天下 提交于 2019-12-25 08:58:47
问题 Right now I have two last problem with the first part of my library. And the first one is this thing not possible in C++ without hack (if I want the constexpr version), it's a derived class counter : class FooBase { protected: static int Counter; }; class Foo : public FooBase { public: static const int Type; }; const int Foo::Type = ++FooBase::Counter; struct FooTest : public Foo {}; Must be in a source file: int FooBase::Counter = 0; Why I need this counter? Well I use it as a type and an

Having trouble checking whether template parameter is something you get when you pass a function's identifier

て烟熏妆下的殇ゞ 提交于 2019-12-25 07:58:53
问题 I want to implement the following function: template<typename Function, typename... Parameters> inline void foo( const Function& kernel_function, bar_t bar Parameters... parameters) { static_assert(/* magic */, "You can only foo() a function, not values of any other type"); / * etc. etc. */ } and I need it to only be called with the identifiers of functions, or with pointers to functions: No lambads or methods or classes with operator() or std::function s. What should I replace /* magic */

Same declaration two different types

拟墨画扇 提交于 2019-12-25 03:46:09
问题 I would like to be able to do this: X<int> type_0; X<int> type_1; and I would like for type_0 and type_1 to be two different types. How can I do it? 回答1: template < typename T, int I > class X; X<int, __LINE__ > x0; X<int, __LINE__ > x1; x0 and x1 will be different types as will any other declarations like this if they are not on the same line in the file. 回答2: You'll need to parameterize on another thing (e.g. an integer?). For example, define X as template <typename T, int N> struct X {...}

Remove simpledb mapWith by meta programming in dev mode

≡放荡痞女 提交于 2019-12-25 02:43:24
问题 I am using simpleDB GORM with my grails application, though simpledb is great its a huge drag while in development mode, every click takes few seconds resulting in not so rapid development. From what I understand simpleDB comes into action only if domain class has following two lines in code String id static mapWith = "simpledb" So, my question is, is it possible to remove/hide these two declaration on the fly from domain classes depending on some kind of flag? Same question asked differently

Invoke a Macro Using A Macro Variable Name

冷暖自知 提交于 2019-12-25 02:16:05
问题 Is it possible in SAS to invoke a call to a macro using a macro variable whose value is the macro's name? Something like the following: %MACRO TEST (macroName, var1, var2, var3, var4, var5); %put LOG: %&macroName(&var1); %MEND; %TEST(testName,testVar1); In response to Richard's answer. I tried following your solution, but am still getting an "apparent invocation of macro YearMonthString not resolved" using the following code: %MACRO YearMonthString(nYear,nMonth); /* Builds a string in the

Self decompressing and executing code in python

扶醉桌前 提交于 2019-12-25 02:09:49
问题 This is the code that creates a compressed file with arbitrary python code in it. The idea is that you paste some kind of python program in the raw_code variable assignment, then run the script which creates a .py file which is functional python code that opens another python shell and executes the compressed code stored within it. import zlib raw_code = """print 'hello world' """ code_prefix = """import zlib import os compressed_code = \"\"\" """ code_postfix = """ \"\"\" os.system('python '

How to refactoring when metaprogramming has side effects?

断了今生、忘了曾经 提交于 2019-12-25 02:04:16
问题 I am using Ruby on Rails 3.2.9 and Ruby 1.9.3-p125. After my previous question I ended up that I have an issue on metaprogramming a self-implemented acts_as_customizable plugin since the related code has side effects on other classes than that "acting as customizable". To summarize the issue: the acts_as_customizable method stated for an Article model "internally" (through metaprogramming) adds a customize method to a Comment model and, in order to save time, Rails doesn't load all those

When to use define_singleton_method v define_method

Deadly 提交于 2019-12-25 01:33:29
问题 In one answer to this question user, mu is too short, explains that you wouldn't want an object's behavior to change too drastically on initialization, which makes sense, you should be able to reason about an object well by reading its definition and being introspective but I had this case in mind: french_colors.yml blue: blue red: rouge ... translations.rb class Translations def initialize(language: "french") data = YAML.load_file("./translations/#{language}.yml") data.each do |k, v| define

How to Support Variadic Template Types in Visual Studio 2017

纵然是瞬间 提交于 2019-12-25 01:33:02
问题 T.C. gave a clever solution here which used this object: template<class... Ts> struct overload : Ts... { using Ts::operator()...; }; template<class... Ts> overload(Ts...) -> overload<Ts...>; struct fallback_t { template<class T> fallback_t(T&&) {} }; Unfortunately I can't get that to compile in Visual Studio 2017. I get the errors: 1> warning C4346: 'Ts::()': dependent name is not a type 1> note: prefix with 'typename' to indicate a type 1> note: see reference to class template instantiation

C++: Multiple Policies calling each other

早过忘川 提交于 2019-12-24 22:10:02
问题 For a policy-based class-design I need some of the policies to call functions that are found in other policies: struct PolicyA { void foo() { // ... } }; struct PolicyB { void bar() { // Need to call foo() here } }; template <class A, class B> MyClass : A, B {}; The two options I see are: Pass MyClass<A, B> to PolicyA and PolicyB as template parameters and do something like dynamic_cast<MyClass<A, B>*>(this)->foo(); . However this may easily get very complicated (especially if it's not only B