Conditional compilation of templates

前端 未结 2 1588
天命终不由人
天命终不由人 2020-12-22 05:00

I am trying to get static_assert to help me avoid null pointers in C++11.

The problem seems to be that C++11 require the compiler to compile templates even if they a

2条回答
  •  不思量自难忘°
    2020-12-22 05:22

    In all C++ standards ever, templates are compiled in two phases. The second phase is instantiation, but compilation can also fail in phase 1. In particular, syntax errors are detected in phase 1.

    In your case, the simpler solution is to leave out the body of the second instantiation.

    Another solution is to use T in the static_assert, so the compiler must delay evaluation to phase 2. Trivially: static_assert(sizeof(T)==0,

提交回复
热议问题