1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <iostream>
#include <map>
using namespace std;
// Our template functor
template <typename T1, typename T2>
struct t_unpair
{
T1& a1;
T2& a2;
explicit t_unpair( T1& a1, T2& a2 ): a1(a1), a2(a2) { }
t_unpair<T1,T2>& operator = (const pair<T1,T2>& p)
{
a1 = p.first;
a2 = p.second;
return *this;
}
};
// Our functor helper (creates it)
template <typename T1, typename T2>
t_unpair<T1,T2> unpair( T1& a1, T2& a2 )
{
return t_unpair<T1,T2>( a1, a2 );
}
// Our function that returns a pair
pair<int,float> dosomething( char c )
{
return make_pair<int,float>( c*10, c*2.9 );
}
// And how we test the pair.
int main()
{
int a;
float b;
unpair( a, b ) = dosomething( 'A' );
cout << a << ", " << b << endl;
return 0;
}
|