Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualkloffy

Posted 14 March 2013 - 01:08 PM

This should be pretty basic template meta programming, but everything I have found on Google isn't quite the thing I am looking for. Suppose I have a template with an empty body that users can specialize: 

template<typename Target, typename Source, typename Enable = void>
struct conversion
{
	static const bool value = false;

	static Target to(Source const& value);
	static Source from(Target const& value);
};

 

And a possible specialization might look like this:

template<typename Target>
struct conversion<Target, std::string>
{
	static const bool value = true;

	static Target to(std::string const& value)
	{
		return boost::lexical_cast<Target>(value);
	}

	static std::string from(Target const& value)
	{
		return boost::lexical_cast<std::string>(value);
	}
};

 

Now, I can check whether a conversion exists by checking conversion<Target, Source>::value. However, I would like to simplify this further, eliminating the need to explicitly define the integral value constant. This can probably be done using a check like sizeof(?) == sizeof(char). However, since there are a few pitfalls, I wonder if someone could point me towards the "best practice" approach.


#2kloffy

Posted 14 March 2013 - 01:07 PM

This should be pretty basic template meta programming, but everything I have found on Google isn't quite the thing I am looking for. Suppose I have a template with an empty body that users can specialize: 
template<typename Target, typename Source, typename Enable = void>
struct conversion
{
	static const bool value = false;

	static Target to(Source const& value);
	static Source from(Target const& value);
};
And a possible specialization might look like this:
template<typename Target>
struct conversion<Target, std::string>
{
	static const bool value = true;

	static Target to(std::string const& value)
	{
		return boost::lexical_cast<Target>(value);
	}

	static std::string from(Target const& value)
	{
		return boost::lexical_cast<std::string>(value);
	}
};
Now, I can check whether a conversion exists by checking conversion<Target, Source>::value. However, I would like to simplify this further, eliminating the need to explicitly define the integral value constant. This can probably be done using a check like sizeof(?) == sizeof(char). However, since there are a few pitfalls, I wonder if someone could point me towards the "best practice" approach.

#1kloffy

Posted 14 March 2013 - 01:06 PM

This should be pretty basic template meta programming, but everything I have found on Google isn't quite the thing I am looking for. Suppose I have a template with an empty body that users can specialize:
 
template<typename Target, typename Source, typename Enable = void>
struct conversion
{
	static const bool value = false;

	static Target to(Source const& value);
	static Source from(Target const& value);
};
And a possible specialization might look like this:
template<typename Target>
struct conversion<Target, std::string>
{
	static const bool value = true;

	static Target to(std::string const& value)
	{
		return boost::lexical_cast<Target>(value);
	}

	static std::string from(Target const& value)
	{
		return boost::lexical_cast<std::string>(value);
	}
};
Now, I can check whether a conversion exists by checking conversion<Target, Source>::value. However, I would like to simplify this further, eliminating the need to explicitly define the integral value constant. This can probably be done using a check like sizeof(?) == sizeof(char). However, since there are a few pitfalls, I wonder if someone could point me towards the "best practice" approach.

PARTNERS