防止编码器错误 - 忘记在 "std::enable_if<>::type" 中添加“::type”(SFINAE)

prevent coder error - forget to add "::type" in "std::enable_if<>::type" (SFINAE)

这是SFINAE的正确代码(demo):-

template<bool b> class C {
    public: template<bool b2=b> typename std::enable_if<b2,void>::type f(){
        cout<<"no!!";
    }
};
int main() {
    //C<false> c1; c1.f(); <-- not compilable (it is good)
    C<true> c2; c2.f();
    return 0;
}

我是 C++ 的新手,还不习惯。
结果,我经常忘记添加 ::type :-

    public: template<bool b2=b> std::enable_if<b2,void> f(){
        cout<<"no!!";
    }

不幸的是,即使它是错误的,它也可以编译为 <false> (demo).

在我糟糕的一天,我什至走得更远:-

    public: std::enable_if<b,void> f(){
        cout<<"no!!";
    }

<false> 仍然可以编译! (demo).

我知道how/why这是错误的,但我在实践中经常犯这样的错误。

问题: 除了编译器警告,这种情况如何防止编码器错误?

C++14 引入了 std::enable_if_t,它不再要求程序员显式引用嵌套类型,因此不允许犯这种错误。您可以为自己的代码决定根本不再使用 enable_if,甚至可以在您自己的源代码中将其列入黑名单。