Why static_assert in template gives me different result with equivalent expressions?

后端 未结 2 1772
逝去的感伤
逝去的感伤 2020-12-21 09:36

I\'ve noticed strange behavior of static_assert:

#include 

template  struct Vec
{
    static_         


        
2条回答
  •  心在旅途
    2020-12-21 10:15

    Everything is not 1,2,3,4 (for which specific specializations exist) will go into the "master" definition, that have to assert for every value of D it will be called.

    So you need an expression that contains D and that is always false, to cause the compiler to evaluate it in dependency of D

    If you just use 0, it will be anymore dependent and the compiler will evaluate as it matches during parsing, causing the assertion to always take place. Even if it will not be the class you will instantiate.

提交回复
热议问题