0

plz write any programme in cpp for simple conversion

conversion

4th Dec 2017, 2:18 PM
Yousaf Ali
Yousaf Ali - avatar
2 Respuestas
0
thanks buddy
4th Dec 2017, 2:51 PM
Yousaf Ali
Yousaf Ali - avatar
0
A simple conversion program :) There may be bugs, I didn't test it too much. Not posted in the playground because stoi doesn't work in the playground. #include <iostream> template<typename From, typename To, typename ConversionFunction> To convert( const From& f, ConversionFunction func ) { return func( f ); } template<typename From, typename To> To convert( const From& f ) { auto defaultConvert = []( const auto& x ){ return static_cast<To>( x ); }; return convert<From, To>( f, defaultConvert ); } class A { public: A() = default; A( int a ):a(a){} operator int()const{ return a; } private: int a = 342; }; int main() { int a = 0; std::string b = "5256"; A c; double d = 24.7; std::cout << convert<std::string, int>( b, []( const auto& x ){ return stoi( x ); } ) << std::endl; std::cout << convert<A, int>( c ) << std::endl; std::cout << convert<double, A>( d ) << std::endl; }
4th Dec 2017, 3:01 PM
Dennis
Dennis - avatar