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

后端 未结 1 474
自闭症患者
自闭症患者 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::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
    struct is_same {
        enum { value = 0 };
    };
    
    template
    struct is_same {
        enum { value = 1 };
    };
    

    0 讨论(0)
提交回复
热议问题