gpt4 book ai didi

c++ - std::declval 和未计算的表达式

转载 作者:行者123 更新时间:2023-11-30 02:20:20 27 4
gpt4 key购买 nike

引用以下Proposing Standard Library Support for the C++ Detection Idiom中的例子:

// primary template handles types that do not support pre-increment
template< class, class = void_t<> >
struct
has_pre_increment_member : false_type { };

// specialization recognizes types that do support pre-increment
template< class T >
struct
has_pre_increment_member<T, void_t<decltype( ++declval<T&>() )>> : true_type { };

如何表达 ++declval<T&>()分类为未评估?

在上面,假设declval()返回 T&Is there a reason declval returns add_rvalue_reference instead of add_lvalue_reference 中所述, 不是表达式 ++T& 的结果(来自 ++declval<T&> )成为 ODR 使用而不是未评估?根据 ODR-use :

a reference is odr-used if it is used and its referent is not known at compile time;

在上面的例子中,在编译时不是不知道引用对象吗?在那种情况下,引用如何与 declval() 一起使用?首先?

最佳答案

how does the expression ++declval<T&>() classify as unevaluated?

因为它在 decltype() 内:

The operand of the decltype specifier is an unevaluated operand.

A function , variable , structured binding , assignment operator or constructor等必须出现在潜在评估表达式中才能被odr-useddecltype()不符合那个标准。

关于c++ - std::declval 和未计算的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49889724/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com