有人能解释一下std::解密是如何运作的吗?我在gcc headers/type_readability(第2248-2262行)中找到了这个实现,它(为了提高可读性而稍微清除了一点):
template<typename _Tp>
struct __declval_protector
{
static const bool __stop = false;
static typename add_rvalue_reference<_Tp>::type __delegate();
};
template<typename _Tp>
typename add_rvalue_reference<_Tp>::type
declval ()
{
static_assert(__declval_protector<_Tp>::__stop, "declval() must not be used!");
return __declval_protector<_Tp>::__delegate();
}我不明白return __declval_protector<_Tp>::__delegate()部分,它是否调用T类型的Rvalue引用的默认初始化器?而且,我也不明白为什么每次调用static_assert时都不调用declval,因为__stop总是错误的(它如何区分未评估的上下文和计算的上下文?)
编辑:根据我现在所理解的,所有这些内容都相当于:
template<typenam _Tp>
struct __declval_protector
{
const bool __stop = false;
};
template<typename _Tp>
typename add_rvalue_reference<_Tp>::type
mydeclval ()
{
static_assert(__declval_protector<_Tp>::__stop, "declval() must not be used!");
}当然,编译器会发出我们不返回任何内容的命令。
发布于 2016-06-15 18:34:57
我不明白为什么每次我给解密打电话时都会调用static_assert,因为__stop总是错误的。
你的前提是错误的。实际上,每次调用declval时都会调用静态断言。诀窍是你绝不能打电话给declval。它只能在未评估的上下文中使用。这就是为什么静态断言存在的原因。
https://stackoverflow.com/questions/37843196
复制相似问题