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

后端 未结 1 482
自闭症患者
自闭症患者 2020-12-13 13:54

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

相关标签:
1条回答
  • 2020-12-13 14:40

    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 };
    };
    
    0 讨论(0)
提交回复
热议问题