SFINAE 尝试使用 bool 给出编译器错误:“模板参数‘T::value’涉及模板参数";
问题描述
我尝试使用 bool
(不同于流行的 void_
技巧):
目标是专门化,其中定义了 static const bool my_value = true;
的类.如果它们已定义 false
或未定义,则不要专门化它.即
当在 B1
上应用上述技巧时,它给出了编译错误:
<块引用>
错误:模板参数‘T::my_value’涉及模板参数
我知道这可以通过其他方式实现.但是,我很想知道,为什么它会在此处给出编译器错误,并且可以在此代码本身中解决吗?
实际上你在做什么是第 §14.5.4/9 节禁止的,它说,
<块引用>部分特化的非类型参数表达式不应涉及部分特化的模板参数,除非参数表达式是一个简单的标识符.
技巧也可以使用 type 作为第二个模板参数,封装 非类型 值,如下所述:
现在它编译罚款.
I tried to implement an SFINAE using bool
(unlike popular void_
trick):
The goal is to specialize, the classes which have static const bool my_value = true;
defined inside it. If they are defined false
or not defined then don't specialize it. i.e.
When applying the above trick on B1
it gives the compilation error:
error: template argument ‘T::my_value’ involves template parameter(s)
I am aware that this can be achieved with alternate ways. However, I am interested in knowing, why it gives compiler error here and can it be solved in this code itself ?
Actually what you're doing is forbidden by section §14.5.4/9 which says,
A partially specialized non-type argument expression shall not involve a template parameter of the partial specialization except when the argument expression is a simple identifier.
The trick could be using a type for second template parameter as well, encapsulating the non-type value, as described below:
Now it compile fines.
这篇关于SFINAE 尝试使用 bool 给出编译器错误:“模板参数‘T::value’涉及模板参数";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!