יום שני, 10 ביוני 2013

std:tie

The std::tie method is the opposite of make_tuple methods.
It is used to retrieve a discrete objects out of a c++ tuple.
The following code demonstrate the use of tie method :

  1: #include <tuple>
  2: class MYObj
  3: {
  4:   public:
  5:     MYObj(std::string pName);
  6:     MYObj();
  7:   public:
  8:     std::string mName;
  9: };
 10: 
 11: MYObj::MYObj(std::string pName)
 12: {
 13:   mName = pName;
 14: }
 15: MYObj::MYObj()
 16: {
 17:   
 18: }
 19: 
 20: int _tmain(int argc, _TCHAR* argv[])
 21: {
 22:   MYObj myObj ("Lion");
 23:   //Create a tuple with 3 different variable types 
 24:   std::tuple <std::string , int , MYObj> myTuple = std::make_tuple ("Cat" , 42 , myObj );
 25: 
 26:   std::string theAnimleName ;
 27: 
 28:   MYObj RetmyObj;
 29: 
 30:   auto thetieTesult = std::tie (theAnimleName, std::ignore, RetmyObj) = myTuple; 
 31: 
 32:   std::cout <<RetmyObj.mName + " - " + theAnimleName;
 33: 

The following code retrieve the myTuple values in to discrete variables .
Note the tie method returns a new tuple construct form  the tie parameters .
the std::ignore constant can be used in order to void getting value for a specific parameter ,in the tie tuple result the voided value is shown as std::ignore
Capture7

אין תגובות:

הוסף רשומת תגובה