是否可以在C ++标准库中实现always_false? [英] Is it possible to implement always_false in the C++ standard library?
问题描述
在某些情况下,您可能会使用 always_false
助手,例如如果尝试实例化某些模板,则会导致无条件的 static_assert
失败:
There are cases where one uses an always_false
helper to e.g. cause unconditional static_assert
failure if instantiation of some template is attempted:
template <class... T> struct always_false : std::false_type {};
template<class T>
struct UsingThisShouldBeAnError {
static_assert(always_false<T>::value, "You should not use this!");
};
此帮助程序是必需的,因为模板定义必须(至少在理论上)具有至少一组模板参数,可以为其生成有效的专业名称,以使程序格式正确:
This helper is necessary because a template definition must (at least theoretically) have at least one set of template parameters for which a valid specialization can be produced in order for the program to be well-formed:
[temp.res]/8 :程序不正确-如果满足以下条件,则无需进行诊断:
[temp.res]/8: The program is ill-formed, no diagnostic required, if:
- 无法为模板生成有效的专业化,并且模板没有实例化,或者
[...]
(编写 static_assert(false,``您不应该使用此!''实例化,这不是故意的.)
(Writing static_assert(false, "You should not use this!");
above would thus be ill-formed and a compiler could always fire the static assert, even without the template being instantiated, which is not the intention.)
以下是涉及此模式的问题的快速样本(包括进一步的解释):
Here is a quick sampling of questions involving this pattern (including further explanation):
将 always_false
作为标准库中的工具可能会很有用,因此我们不必不断地编写它.但是,以下问题的答案让我怀疑这是否可能:
It might be useful to have always_false
as a tool in the standard library so we don't have to constantly write it again. However, the answer to the following question makes me wonder whether this is even possible:
有一个论点(也针对[temp.res]/8),即 std :: enable_if_t< T>
始终是 void
或不是类型,并且任何人进一步对其进行专门化都是非法的.因此,依赖于理论上的专业性"的模板被称为模板". std :: enable_if
的代码,以避免[temp.res]/8子句实际上导致程序格式错误,无需诊断.
There the argument is made (also with respect to [temp.res]/8) that std::enable_if_t<T>
is always either void
or not a type and that it is illegal for anyone to specialize it further. Therefore, a template that relies on the theoretical "specializability" of std::enable_if
to avoid the [temp.res]/8 clause actually causes the program to be ill-formed, no diagnostic required.
回到我的问题:如果该标准提供了 always_false
,它将不得不禁止图书馆用户照常对其进行专业化(出于明显的原因).但是根据上述推理,这会破坏 always_false
的全部要点(即,理论上它可以专门用于 std :: false_type
之外的其他事物)-关于[temp.res]/8,与直接使用 std :: false_type
的效果相同.
Coming back to my question: If the standard provided always_false
, it would have to forbid library users from specializing it as usual (for obvious reasons). But by the above reasoning, that would defeat the whole point of always_false
(namely that it could theoretically be specialized to something other than std::false_type
) - with respect to [temp.res]/8 it would be the same as using std::false_type
directly.
我在这个推理上错了吗?还是标准库实际上不可能以有意义/有用的方式(无需更改核心语言)提供 always_false
吗?
Am I wrong in this reasoning? Or is it actually impossible for the standard library to provide always_false
in a meaningful/useful way (without core language changes)?
推荐答案
释义Jarod的想法,可能是
To paraphrase Jarod's idea, It could be something like
template <class... T> struct always_false : std::false_type {};
template <> struct always_false</* implementation defined */> : std::true_type{};
/*实现定义的*/
可以由 std :: __ ReservedIdentifer
填充.用户代码无法访问它,因为标识符是保留给库的,但是存在一种特殊化,即 true
.那应该避免对专业化中的ODR和lambda产生疑问.
Where /* implementation defined */
can be filled by std::_ReservedIdentifer
. User code can't access it, since the identifier is reserved to the library, but there exists a specialization that is true
. That should avoid questions about the ODR and lambdas in specializations.
这篇关于是否可以在C ++标准库中实现always_false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!