Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualkloffy

Posted 14 March 2013 - 04:48 PM

Of course, now that I think about it, this might be a much more elegant solution:
template<typename Target, typename Source, typename Enable = void>
struct conversion
{
	static_assert(sizeof(Target) == 0 || sizeof(Source) == 0, "Must specialize conversion!");

	Target operator()(Source const& value) const;
};

template<typename Target, typename Source, typename Conversion = conversion<Target, Source>>
Target conversion_cast(Source const& value, Conversion conversion = Conversion())
{
	return conversion(value);
}

template<>
struct conversion<int, float>
{
	int operator()(float const& value) const
	{
		return static_cast<int>(value);
	}
};

template<>
struct conversion<float, int>
{
	float operator()(int const& value) const
	{
		return static_cast<float>(value);
	}
};
 
The only "downside" is that the "to" and "from" conversions are no longer grouped together. I guess both operations go hand in hand, so I wanted to motivate users to write both. However, it caused more problems than it solved (especially since the previous code does not enforce that both functions are present either).

#2kloffy

Posted 14 March 2013 - 03:29 PM

Of course, now that I think about it, this might be a much more elegant solution:

template<typename Target, typename Source, typename Enable = void>
struct conversion
{
	static_assert(sizeof(Enable) == 0, "Must specialize!");

	Target operator()(Source const& value) const;
};

template<typename Target, typename Source, typename Conversion = conversion<Target, Source>>
Target conversion_cast(Source const& value, Conversion conversion = Conversion())
{
	return conversion(value);
}

template<>
struct conversion<int, float>
{
	int operator()(float const& value) const
	{
		return static_cast<int>(value);
	}
};

template<>
struct conversion<float, int>
{
	float operator()(int const& value) const
	{
		return static_cast<float>(value);
	}
};

 

The only "downside" is that the "to" and "from" conversions are no longer grouped together. I guess both operations go hand in hand, so I wanted to motivate users to write both. However, it caused more problems than it solved (especially since the previous code does not enforce that both functions are present either).


#1kloffy

Posted 14 March 2013 - 03:29 PM

Of course, now that I think about it, this might be a much more elegant solution:
template<typename Target, typename Source, typename Enable = void>
struct conversion
{
	static_assert(sizeof(Enable) == 0, "Must specialize!");

	Target operator()(Source const& value) const;
};

template<typename Target, typename Source, typename Conversion = conversion<Target, Source>>
Target conversion_cast(Source const& value, Conversion conversion = Conversion())
{
	return conversion(value);
}

template<>
struct conversion<int, float>
{
	int operator()(float const& value) const
	{
		return static_cast<int>(value);
	}
};

template<>
struct conversion<float, int>
{
	float operator()(int const& value) const
	{
		return static_cast<float>(value);
	}
};
The only "downside" is that the "to" and "from" conversions are no longer grouped together. I guess both operations go hand in hand, so I wanted to motivate users to write both. However, it caused more problems than it solved (especially since the previous code does not enforce that both functions are present either).

PARTNERS