"Both have tuple, so why not compare and contrast them?" led to this write up.
Cutting right to the chase, here are the similarities and differences between swift and C++ tuple.
Basically both language tuples set out to achieve the same. To provide an anonymous structure like construct where zero or more heterogenous elements can be used without worrying about creating a class or structure. Used mainly with functions who have multiple elements to be returned.
You can read all about Swift tuple here and C++ tuple here and lots of other online documents. So in this post I am not going to go into the syntactical differences. Rather, lets look at one single seemingly hidden usage difference keeping this blog post as crisp and clean as possible.
C++
In C++ some people thing using anonymous structs leads to long long hours of excruciating debugging, which could be reduced by using clear, visible named structs.
The main irking factor regarding C++ tuple according to my humble opinion is the lack of programmer readable member names for each element.
For e.g If you have to access elements of a C++ tuple, you would have to do this:
Swift:
Now that C++ code is looked into, it should be relieving to know that swift eliminates that bothersome aspect by providing member names.
Cutting right to the chase, here are the similarities and differences between swift and C++ tuple.
Basically both language tuples set out to achieve the same. To provide an anonymous structure like construct where zero or more heterogenous elements can be used without worrying about creating a class or structure. Used mainly with functions who have multiple elements to be returned.
You can read all about Swift tuple here and C++ tuple here and lots of other online documents. So in this post I am not going to go into the syntactical differences. Rather, lets look at one single seemingly hidden usage difference keeping this blog post as crisp and clean as possible.
C++
In C++ some people thing using anonymous structs leads to long long hours of excruciating debugging, which could be reduced by using clear, visible named structs.
The main irking factor regarding C++ tuple according to my humble opinion is the lack of programmer readable member names for each element.
For e.g If you have to access elements of a C++ tuple, you would have to do this:
#include
std::tuple GetCustomerInfo(unsigned long BillID)
{
//retrieve bill details
return std::make_tuple(Name, Phone, Amount);
}
int main()
{
auto cust = GetCustomerInfo(BillID); //assume BillID is valid
std::cout << "Name:" << std::get<0>(cust) << endl; // usage of const numbers instead of proper member names is a well intentioned trap for longer crazier debugging hours.
std::cout << "Contact:'" << std::get<1>(cust) << endl;
std::cout << "Amount:" << std::get<2>(cust) << endl;
}
Now that C++ code is looked into, it should be relieving to know that swift eliminates that bothersome aspect by providing member names.
func GetCustomerInfo(BillID: UInt) -> (Name: String, Contact: String, BillAmount: Float)?{ //return optional tuple value
//retrieve values here
//if bill id is not found in the database return nil
return (Name, Contact, Amount);
}
//in another function
if let cust = GetCustomerInfo(BillID) { //if not nil, process value
print("Name: \(cust.Name) \n Contact: \(cust.Contact) \n Amount: \(cust.BillAmount)";
}
No comments:
Post a Comment