1

Topic: Help with

Kind day, Prompt, as it is correct to implement the following: There is some list untied among themselves (without the general ancestor) types T1, T2, T3..., each of which has static method StaticMeta. It is necessary to write function bool Validate <T1, T2, T3> (const MetaInfo& staticMeta) which compares staticMeta to each of types (staticMeta == T1:: StaticMeta ()). Something cannot be made the necessary overloads. A code sample (it is a little , but reflects the task) #include <iostream> #include <string> #include <memory> #include <utility> #include <cassert> #define GENERATE_META (type) \public: \static const MetaInfo& StaticMeta () \{\static MetaInfo meta {#type}; \return meta; \} struct MetaInfo {const std:: string _id; explicit MetaInfo (std:: string ti): _id {std:: move (ti)} {} bool operator == (const MetaInfo& rhs) const {return _id == rhs. _ id;} bool operator! = (const MetaInfo& rhs) const {return operator == (rhs);} };//Inheritance is just for example//In real project code is generated by tools struct Base {protected: Base () = default;}; struct MyType1: Base {GENERATE_META (MyType1) int _dummy = 1;}; struct MyType2: Base {GENERATE_META (MyType2) float _dummy = 2.f;}; struct MyType3: Base {GENERATE_META (MyType3) std:: string _dummy = "gg";}; class Table {public: template <typename T> static Table Create () {auto data = std:: make_shared <T> (); auto meta = &T::StaticMeta (); return {data, meta};} const std::shared_ptr<Base>& Data () const {return _data;} const MetaInfo& Meta () const {return * _meta;} private: Table (std:: shared_ptr <Base> data, const MetaInfo * meta): _data {std:: move (data)}, _meta {meta} {} private: std:: shared_ptr <Base> _data; const MetaInfo * const _meta = nullptr;} ; template <typename T> void Print (const Table& table) {assert (table. Meta () == T:: StaticMeta ()); const auto& data = *static_cast <T *> (table. Data ().get ()); std:: cout <<table. Meta (). _id <<"->" <<data. _ dummy <<"\n";} ///////////////////////////////////////////////////////////////////// template <typename T> bool Validate (const Table& table) {if (table. Meta () == T:: StaticMeta ()) {Print <T> (table); return true;} return false;} template <typename T, typename... Tables> bool Validate (const Table& table) {if (Validate <T> (table)) return true; return Validate <Tables...> (table);} int main () {const auto t1 = Table:: Create <MyType1> (); const auto t2 = Table:: Create <MyType2> (); const auto t3 = Table:: Create <MyType3> (); const auto r1 = Validate <MyType1, MyType2> (t1); assert (r1); const auto r2 = Validate <MyType1, MyType2> (t2); assert (r2); const auto r3 = Validate <MyType1, MyType2> (t3); assert (! r3); return 0;}

2

Re: Help with

Hello, SaZ, you wrote: SaZ> Something cannot be made the necessary overloads. And what for generally here to do overloads? They superfluous and favor do not carry. Simply rename the first method Validate and all turns out://check of one element template <typename T> bool Validate_1 (const Table& table) {if (table. Meta () == T:: StaticMeta ()) {Print <T> (table); return true;} return false;}//list check template <typename T = void, typename... Tables> bool Validate (const Table& table) {if (Validate_1 <T> (table)) return true; return Validate <Tables...> (table);} template <> bool Validate (const Table& table) {return false;} As though here at once it is visible that functions for check of one element and for check of the list from one element - different. For the list from one element and a element - too like different types. SaZ> bool operator! = (const MetaInfo& rhs) const SaZ> {SaZ> return operator == (rhs); SaZ>} SaZ>}; Something this fragment too looks suspiciously wink

3

Re: Help with

4

Re: Help with

Hello, SaZ, you wrote: SaZ> As though does not help, tried already. - https://ideone.com/24vOZu. Too tried - helps: https://ideone.com/mQW8Sh Someone is not able to do Copy&Paste? A joke. But it nevertheless important skill in programming. At some, speak, such method turns out to write all program entirely smile

5

Re: Help with

Hello, watchmaker, you wrote: W> Hello, SaZ, you wrote: SaZ>> As though does not help, tried already. - https://ideone.com/24vOZu. W> Too tried - helps: https://ideone.com/mQW8Sh W> Someone is not able to do Copy&Paste? A joke. But it nevertheless important skill in programming. At some, speak, such method turns out to write entirely all program Thanks, it was corrected. And can explain about typename T = void and why it helped?

6

Re: Help with

Hello, SaZ, you wrote: SaZ> And can explain about typename T = void and why it helped? It not the most important part: it is possible and without it. =void the termination condition through specialization (and it for functions is possible only full) is used to set default argument instructions simply. More often, of course, the recursion termination becomes through count of parameters and the same overload. But here at you all functions have exactly same list of arguments (const Table& table), and for them this method is closed. Though it is possible to bypass it if to enter into the function signature these types, adding, for example, fictitious argument. Then all functions will have different signatures and an overload will work: template <typename... Types> using TupleTypeList = std:: tuple <Types*...> *;//we make the list on the basis of existing std:: : tuple not to write for an example the class bool ValidateTuple (const Table& table, TupleTypeList <>) {//an overload for the empty type list return false;} template <typename Head, typename... Tail> bool ValidateTuple (const Table& table, TupleTypeList <Head, Tail...>)//analysis of the general case with the nonblank type list {if (Validate_1 <Head> (table)) return true; return ValidateTuple (table, TupleTypeList <Tail...> {});} template <typename... Tables> bool Validate (const Table& table) {return ValidateTuple (table, TupleTypeList <Tables...> {});} the null pointer (the optimizer throws out it then) Here is simply transferred to a tuple which parameters are that list of classes which needed to be checked up. Upd: this code - simply demonstration, I do not say that so it is necessary to write smile

7

Re: Help with

Hello, watchmaker, you wrote: W>... W> Upd: This code - simply demonstration, I do not say that so it is necessary to write Thanks, now all is clear. I understood that problems were just in the absence of arguments with  types, but did not know, how it to resolve.