1

参考 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 返回 add_rvalue_reference 而不是 add_lvalue_reference 中讨论的那样,表达式的结果++T&(由 产生++declval<T&>)不会变成 odr-used 而不是未评估?根据ODR 使用

如果使用了引用并且在编译时不知道其引用对象,则该引用是 odr-used;

在上述情况下,不是在编译时不知道所指对象吗?在那种情况下,如何首先使用引用declval()

4

1 回答 1

3

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

Because it's within decltype():

The operand of the decltype specifier is an unevaluated operand.

A function, variable, structured binding, assignment operator or constructor, etc. must appear in a potentially evaluated expression in order to be odr-used. decltype() doesn't meet that criteria.

于 2018-04-18T01:28:57.070 回答