• Advertisement
Sign in to follow this  

typedefs in Concepts or in Traits?

This topic is 3722 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()

		// #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();
	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>
	// 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
Sign in to follow this  

  • Advertisement