How to check if two types are same at compiletime(bonus points if it works with Boost strong typedef)

15,390

Use std::is_same. std::is_same<T,U>::value will be true if T and U are the same type, false otherwise.

If you don't have C++11, it's easy to implement as this

template<class T, class U>
struct is_same {
    enum { value = 0 };
};

template<class T>
struct is_same<T, T> {
    enum { value = 1 };
};
Share:
15,390

Related videos on Youtube

NoSenseEtAl
Author by

NoSenseEtAl

Loving C++ because it gives me a warm FUBU feeling, also CHF :P aka: money for programming and high level abstractions for (almost )free. :P I dont hate C that much, but I fu*king hate C APIs

Updated on June 04, 2022

Comments

  • NoSenseEtAl
    NoSenseEtAl over 1 year

    I was wondering if it is possible to check if 2 types are same at compile time. What I came up with is(idk if it works because it feels hackish and IDK standard that good so IDK what to look for when testing).

    #include <boost/strong_typedef.hpp>
    BOOST_STRONG_TYPEDEF(double, cm);
    BOOST_STRONG_TYPEDEF(double, inch);
    template<typename T, typename U>
    static constexpr void __help() 
    {
    }
    template<typename T, typename U>
    class AreSameType
    {
        public:
        constexpr operator bool()
        {
         return &__help<T,U> == &__help<U,T>;
        };
    };
    

    usage :

    int main()
    {
            static_assert(AreSameType<double,float>()== false, "oh noes1");
            static_assert(AreSameType<double,double>()== true, "oh noes2");
            static_assert(AreSameType<int*,double*>()== false, "oh noes3");
            static_assert(AreSameType<double*,double>()== false, "oh noes4");
            static_assert(AreSameType<const double,double>()== false, "oh noes5");
            static_assert(AreSameType<inch,cm>()== true, "oh expected"); //fires
    }
    

    So

    1) is there a better way to it?
    2) is this address of a function hack guaranteed to work by standard(I would bet not :))?

    • R. Martinho Fernandes
      R. Martinho Fernandes about 11 years
      And there's boost::is_same from Boost.TypeTraits.
    • Xeo
      Xeo about 11 years
      How do you want it to work with BOOST_STRONG_TYPEDEF?
    • NoSenseEtAl
      NoSenseEtAl about 11 years
      like std::is_same, just tested it, it is really really strong typedef
    • Matthieu M.
      Matthieu M. about 11 years
      I support Xeo's remark here, the very idea of BOOST_STRONG_TYPEDEF is to create a type that cannot be accidentally misconstrued with its "origin". Did you thought about using Boost.Unit for your units ?
    • NoSenseEtAl
      NoSenseEtAl about 11 years
      @MatthieuM - yeah, that is what I want, maybe source is confusing, sorry for that
  • Xeo
    Xeo about 11 years
    Or as easy as using boost::is_same.
  • NoSenseEtAl
    NoSenseEtAl about 11 years
    cool, i wanted to try something like that but I had troubles figuring out how to specialize(wrong word?) template when both types are same... i tried stupid template<tyname T, tyname T> nad template <>
  • Matthieu M.
    Matthieu M. about 11 years
    @NoSenseEtAl: specialize is the right word. Here you see a partial specialization (ie, there are still template parameters) whilst template <> indicates a full specialization (no more template parameters: thus all types/values are known).
  • thejoshwolfe
    thejoshwolfe over 7 years
    #include <type_traits> also reference: en.cppreference.com/w/cpp/types/is_same
  • NoSenseEtAl
    NoSenseEtAl almost 6 years
    note that since c++17 we have a nicer is_same_v that removes the need for ::value
  • Zhang
    Zhang about 5 years
    What a genius you are.