“无法专门化别名模板”最简单的 SFINAE bool 条件的错误

发布于 2025-01-15 01:33:00 字数 1739 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扎心 2025-01-22 01:33:00

如下情况又如何呢?

template<class T, bool COND=0> class A
{
public:
    template< bool CC=COND >
        std::enable_if_t<!CC> METHOD() const { };

    template< bool CC=COND >
        std::enable_if_t<CC> METHOD() const { };
};

我的意思是...如果您想通过 std::enable_if 启用/禁用类的方法,您必须检查依赖于该方法的模板(类型或值)的测试,而不是班级的。

所以

std::enable_if_t<!COND> 

不起作用,因为 COND 是该类的模板值;您必须使用该方法的模板值,因此您可以添加一个模板值 CC,您可以默认为 COND

template< bool CC=COND >

并选中 CC而不是COND

std::enable_if_t<!CC>

What about as follows?

template<class T, bool COND=0> class A
{
public:
    template< bool CC=COND >
        std::enable_if_t<!CC> METHOD() const { };

    template< bool CC=COND >
        std::enable_if_t<CC> METHOD() const { };
};

I mean... if you want enable/disable a method of a class through std::enable_if, you have to check a test that depend from a template (type or value) of the method, not of the class.

So

std::enable_if_t<!COND> 

doesn't works because COND is a template value of the class; you have to use a template value of the method, so you can add a template value CC, that you can default to COND

template< bool CC=COND >

and check CC instead of COND

std::enable_if_t<!CC>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文