本文介绍了为什么这种替换失败会产生错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在模板特化中,我有一个带有 enable_if 参数的模板参数,该参数导致 enable_if 没有类型"成员,因此模板特化应该失败,但不会产生错误:

In a template specialization I have a template argument with an enable_if with parameter that results in the enable_if not having a 'type' member, and so that template specialization should fail, but not create an error:

#include <type_traits>


template <typename value_t_arg, typename T = void>
struct underlyingtype
{
    using underlyingtype_t = value_t_arg;
};

template <typename value_t_arg>
struct underlyingtype < value_t_arg, typename std::enable_if<false>::type>
// std::enable_if<false> has no 'type' member, and so substitution should fail,
// but no create an error, right?
{
    //using underlyingtype_t = value_t_arg::integral_t;
};

为什么会在此处创建错误?

Why is there an error created here?

推荐答案

您的代码格式错误(无需诊断),因为无论模板参数如何,条件始终为假,这意味着特化对于每个可能的模板参数.

Your code is ill-formed (no diagnostic required) because the condition is always false regardless of the template argument, meaning the specialization would be ill-formed for every possible template argument.

[temp.res.general]/6.1

程序格式错误,无需诊断,如果:

The program is ill-formed, no diagnostic required, if:

——不能为模板生成有效的特化......并且模板没有被实例化,......

— no valid specialization can be generated for a template ... and the template is not instantiated, ...

部分专业化似乎算作模板";就本节而言.

Partial specializations appear to count as "templates" for the purposes of this section.

这篇关于为什么这种替换失败会产生错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:00