Sign in to follow this  

typedefs in Concepts or in Traits?

This topic is 3629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm relatively new to making my own Concepts and Traits classes, but I use the boost ones with some regularity. I'm curious as to what people think best practice is when coding with Concepts and Traits. Do you put required typedefs in a traits class or in the Concept class itself? Are Concept classes for checking interfaces only while traits are used to access typedefs, and if so, do you check to see if the user specialized the traits class in the Concept?
template <typename T>
struct SpecialListConcept
{
	void constraints()
	{
		boost::function_requires<ListConcept>(T);

		// #1 - do this?
		typedef typename T::required_type required_type;

		// #2 - or this?
		typedef special_list_traits<T> required_traits;
		typedef typename required_traits::required_type required_type;

		bool i = value.required_method();
	}
private:
	T value;
};
What do you perceive as the pros and cons in each case? For me personally, the first solution nearly always means wrapping existing classes with a tiny adapter. e.g. If the generic algorithm required that X models SpecialListConcept which was a refinement of ListConcept, it would require me to do the following to adapt std::list to be a model of SpecialListConcept.
template <typename T, typename A = std::allocator<T> >
class my_custom_list : public std::list<T, A>
{
public:
	// extra typedef required by generic algorithm
	typedef my_custom_type required_type;

	int required_method();
};
But using traits I would have to also specialize:
template <typename T, typename A>
struct special_list_traits<my_custom_list<T, A> >
{
	typedef my_custom_type required_type;
};
The benefit to traits seems to be when you can't modify an existing class, but want to adapt it to model a certain Concept. Thoughts?

Share this post


Link to post
Share on other sites

This topic is 3629 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this