compile-time function for checking type equality

后端 未结 2 1390
谎友^
谎友^ 2020-12-30 04:15

I need to implement self contained compile-time function for checking type equality (function template without arguments bool eqTypes()).

sel

2条回答
  •  旧时难觅i
    2020-12-30 05:00

    Here's how you can do it in C, without any magic GCC extensions:

    #define CHECKED_TYPE(original_type, p) ((conversion_type*) (1 ? p : (original_type*) 0))
    

    E.g:

    void *q = CHECKED_TYPE(int, &y);
    

    Will trigger a compile error if y is not int.
    For explanation, see here.

提交回复
热议问题