[ConceptCheck] Same type concept

I'd like a concept check to check for two types being the same type. I can see how to write such a concept, but this is such an obvious requirement that either there should be one already, or there's some blindingly obvious reason why you never need one, a reason that escapes me. A little guidance would be appreciated. Thanks, Rob.

On 8/24/2014 6:08 PM, Robert Jones wrote:
I'd like a concept check to check for two types being the same type.
I can see how to write such a concept, but this is such an obvious requirement that either there should be one already, or there's some blindingly obvious reason why you never need one, a reason that escapes me.
A little guidance would be appreciated.
Look at boost::is_same in the type traits library. Also modern C++11 has std::is_same.

Thanks Edward.... at risk of looking even more foolish, I've looked at that, and other traits, but think I'm missing something obvious in expressing a type trait as a concept! - Rob. On 24 August 2014 23:44, Edward Diener <eldiener@tropicsoft.com> wrote:
On 8/24/2014 6:08 PM, Robert Jones wrote:
I'd like a concept check to check for two types being the same type.
I can see how to write such a concept, but this is such an obvious requirement that either there should be one already, or there's some blindingly obvious reason why you never need one, a reason that escapes me.
A little guidance would be appreciated.
Look at boost::is_same in the type traits library. Also modern C++11 has std::is_same.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-- ACCU - Professionalism in programming - http://www.accu.org

the obvious thing you're missing is that a "Concept" is just a glorified name for some static asserts. So you can get what you want by using template<typename T, typename U> struct my_class { static_assert(std::is_same<T, U>::value, "Types differ!"); .... }; template<typename T, typename U> int x(T &t; U& u){ static_assert(std::is_same<T, U>::value, "Types differ!"); ... code here } If you want to make an "official looking" concept you can look at boost concept library which describes how to do this. The short version is template<typename T, typename U> struct MyConcept { static_assert(std::is_same<T, U>::value, "Types differ!"); ... all the other type requirements }; Then you could change my class to look like template<typename T, typename U> struct my_class : public MyConcept<T, U> { .... }; template<typename T, typename U> int x(T &t; U& u){ typedef MyConcept<T, U> concept_check; ... code here } Take a look at the Boost concept library which explains all this better. Robert Ramey -- View this message in context: http://boost.2283326.n4.nabble.com/ConceptCheck-Same-type-concept-tp4666862p... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (3)
-
Edward Diener
-
Robert Jones
-
Robert Ramey