The test class
1: class MYObj2: {3: public:4: MYObj(std::string pName);5: public:6: std::string mName;7: };8:9: MYObj::MYObj(std::string pName)10: {11: mName = pName;12: }
An example for simple C++ tuple:
1: MYObj myObj;2: //Create a tuple with 3 different variable types3: auto myTuple = std::make_tuple ("Zvika" , 42 , myObj );4: //Get the first member of the tuple5: auto myName = std::get<0>(myTuple);6: std::cout << myName;
Use of forward_as_tuple in order to insert to the tuple right values
1: MYObj myObj ("Lion");2: //Create a tuple with 3 different variable types3: auto myTuple = std::make_tuple ("Cat" , 42 , myObj );4: //Create a tuple with 3 different variable types of right values5: auto myTupleAsRightValue = std::forward_as_tuple ("cat" , 42 , myObj );6: //Chaneg the object state7: myObj.mName = "weasel";8: //Get the 3 member of the tuple9: auto myName = std::get<2>(myTuple);10:11: std::cout << myName.mName <<"\r\n";12: //Get the 3 member of the tuple of right values13: auto myNameAsRightValue = std::get<2>(myTupleAsRightValue);14:15: std::cout << myNameAsRightValue.mName <<"\r\n";
The result:
Lion
weasel
Declare the tuple without the auto keyword
1: MYObj myObj ("Lion");2: //Create a tuple with 3 different variable types3: std::tuple <std::string , int , MYObj> myTuple = std::make_tuple ("Cat" , 42 , myObj );4: //Create a tuple with 3 different variable types of right values5: int theVal = 42;6: std::string theStr = "cat";7:8: std::tuple <std::string &, int &, MYObj &> myTupleAsRightValue = std::forward_as_tuple (theStr , theVal , myObj );9:
Reference to Tuple:
http://en.cppreference.com/w/cpp/utility/tuple/forward_as_tuple
אין תגובות:
הוסף רשומת תגובה