0
plz write any programme in cpp for simple conversion
conversion
2 Answers
0
thanks buddy
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;
}