c++ - How to create an argument object -
After the text "itemprop =" text ">
In Python we make a" arguments objection "to call any function that receives such a sequence of arguments The problem is: I can say:
arg = (42, true, "hello") f1 (* args) f2 (* args) ++ is facing one I have the usual function f with many logic and with 16 different specializations, and I can clearly tell which version a Read the value of I'm compelled to call enum : switch (type) {case xtype :: BOOL: f & lt; Bool & gt; (arg1, arg2, arg3, ..., arg9); break; Case xtype :: INT8: f & lt; Int8_t & gt; (Arg1, arg2, arg3, ..., arg9); break; Case xtype :: UINT8: f & lt; Uint8_t & gt; (arg1, arg2, arg3, ..., arg9); break; Case xtype :: INT16: f & lt; Int16_t & gt; (arg1, arg2, arg3, ..., arg9); break; ... Case xtype :: Flat: F & LT; Float & gt; (arg1, arg2, arg3, ..., arg9); break; } I was recently given another function, g on which I need to follow the same pattern: switch (type) {case xtype :: BOOL: g What is the easiest way to wrap this type-selection code into some common functions? Or, when I need to change the argument of f or g , how can I make my life easier? For example: invitation (type, F, AG1, AGR2, AG3, ..., AG9); Call (type, g, rg1, rg2, ag3, ..., ag 9); The logic types of f and g can not be the same. Update
I added a reply to my own on how I solved it
After the
You can create a list of types and send accordingly with variadic templates.
#include & lt; Iostream & gt; # Include & lt; Stdexcept & gt; Class type identifier {public: typed text unsigned integer; Enum value {bull = 1, int 8, UInt8, int 32, UInt32, Int64, UInt64, float, double, string, unknown = 0}; Template & lt; Value ... ID & gt; Structure list type {}; Typitic list type & lt; Bull, Intel 8, UInt8, Int 32, UInt32, Int64, UInt64, Float, Double, String, // Always Previous Value: Unknown & gt; List; Public: Integer ID; TypeIdentifier (integer value = unknown): id (value) {} template & lt; Typename function, typename ... T & gt; Typename Functor :: result_type despatch (Konstictor End, T & amp; ...); }; // reminder // ============================================= =============================== Name of the Type TypeIdentifierDispatch {Template & lt; typename functor, typeIdentifier :: value i, typeIdentifier :: value ... id & gt; Structure assessment; Template & lt; Typename Functor & gt; Struct evaluation & lt; Functor, TypeIdentifier :: Unknown & gt; {Template & lt; Typename ... T & gt; Fixed typename functor :: RESULT_TYPE applied (TypeIdentifier :: integer ID, constant functor & amp;; & amp;; & amp; ... logic) {std :: runtime_error ("unknown type"); }}; Template & lt; Typename Functor, TypeIdentifier :: Price I, TypeIdentifier :: Value ... Ids & gt; Struct {template & lt; Typename ... T & gt; Rate it. Fixed typename Functor :: result_type applied (TypeIdentifier :: integer ID, CONSTFactory and Factor, T & amp; ... arguments) {if (id == I) FOLDER (std :: forward ( Argument) ..); And evaluate the return & lt; Functor, Ids ... & gt; :: Apply (ID, Factor, Study :: Forward & lt; T & gt; (Logic) ...); }}; Template & lt; typename Functor, TypeIdentifier :: Value ... ID, Type name ... T & gt; Inline Typname Future :: Results_type Evaluation (Typidentifier :: Integer ID, Constant Function and Factor, TypInndifier :: List Type Type, LT; ID ... ADA ..., T & amp; and ... Arguments) & lt; Function, ID ... & Gt; :: Apply (ID, Factor, Study :: Forward & lt; T & gt; (Logic) ...); }} // namespace typewriter dispatch template & lt; Typename function, typename ... t & gt; Typename Functor :: result_type typeIdentifier :: Removal (Const. Funkter and Materkotter, T & amp; ... Arguments) {Return Type Identifier Dispatch :: Evaluate (ID, Factor, Typendififier :: List), std :: forward & lt; T & gt; ( Argument) ...); } Add struct {typedef} double result_type; Double Operator (Double A, Double B) Const {Return A + B; }}; int main () {TypeIdentifier id (TypeIdentifier :: int32); Std :: cout & lt; & Lt; Id.dispatch (add (), 1.0, 1.5) & lt; & Lt; Std :: endl; } Note: In the above code, a fun doctor is involved. You can use the function signature or std :: function instead. Kennetm wrote some useful work-properties (see)
Comments
Post a Comment