简体   繁体   English

C ++ 0x:条件运算符,xvalues和decltype

[英]C++0x: conditional operator, xvalues, and decltype

I am reposting a comp.std.c++ Usenet discussion here because that group has become very unreliable. 我在这里重新发布comp.std.c ++ Usenet讨论 ,因为该组变得非常不可靠。 The last few posts I've submitted there have gone into the void, and activity has all but ceased. 我在那里提交的最后几篇文章已经进入虚空状态,活动几乎停止了。 I doubt I've been banned and/or everyone else just lost interest. 我怀疑我被禁止和/或其他所有人都失去了兴趣。 Hopefully all interested people will find this discussion, and there will be a general migration. 希望所有感兴趣的人都能找到这个讨论,并且会有一般的迁移。 Maybe then they will appoint a new moderator. 也许他们会任命一位新主持人。


Hello! 你好!

With my current interpretation of draft N3126 wrt the conditional operator and xvalues, I expect the following assertions to hold: 根据我目前对条件运算符和xvalues的N3126草案的解释,我希望以下断言能够成立:

 int i = 0;
 int& j = true? i : i;
 int&& k = true? std::move(i) : std::move(i);   // #2
 assert(&i == &j); // Holds since C++98
 assert(&i == &k); // Should this hold as well?

5.16/4 says: 5.16 / 4说:

If the second and third operands [to the conditional operator] are glvalues of the same value category and have the same type, the result is of that type and value category [...] 如果[到条件运算符]的第二个和第三个操作数是相同值类别的glvalues并且具有相同的类型,则结果是该类型和值类别[...]

Though, it doesn't clearly say that the resulting glvalue refers to one of the objects the glvalue operands referred to -- or is this implied because otherwise it would return a prvalue? 虽然,它没有明确说明得到的glvalue是指glvalue操作数所引用的对象之一 - 或者这是暗示的,否则它会返回一个prvalue? Using GCC 4.5.1 in C++0x mode the second assertion fails. 在C ++ 0x模式下使用GCC 4.5.1,第二个断言失败。 The reference k seems to refer to some temporary object. 引用k似乎是指一些临时对象。 Can somebody clarify whether the comiler is allowed to create such a temporary in case both operands around the colon are xvalues of the same type? 有人可以澄清是否允许编译器创建这样一个临时代码,以防冒号周围的操作数是同一类型的xvalues吗?

I'm currently assuming GCC is buggy and/or not up-to-date with respect to xvalues. 我目前假设GCC是错误的和/或没有关于xvalues的最新版本。

The followup question is: Wouldn't it be nice to be able to detect the value category of an expression? 后续问题是:能够检测表达式的值类别不是很好吗? If we ignore the conditional operator we can detect the value category of an expression with decltype. 如果我们忽略条件运算符,我们可以使用decltype检测表达式的值类别。 But what is 但是是什么

 bool xvalue = std::is_rvalue_reference<
   decltype( true ? std::move(i) : std::move(i) ) >::value;

supposed to yield? 应该屈服? Using GCC 4.5.1, the xvalue variable is initialized with false. 使用GCC 4.5.1,xvalue变量初始化为false。 Is this conforming to the current standard draft? 这符合当前的标准草案吗?

TIA, Sebastian TIA,塞巴斯蒂安

I think GCC 4.5.1 is nonconforming wrt §5.16/4. 我认为GCC 4.5.1是不合格的§5.16/ 4。 Have you filed a bug report ? 你有没有提交错误报告

Anyway, I think it is conforming with that ternary operator code. 无论如何,我认为它符合那个三元运算符代码。 decltype is defined by §7.1.6.2/4: decltype由§7.1.6.2/ 4定义:

The type denoted by decltype(e) is defined as follows: decltype(e)表示的类型定义如下:

  • if e is an unparenthesized id-expression or a class member access (5.2.5), decltype(e) is the type of the entity named by e. 如果e是未表示的id-expression或类成员访问(5.2.5),则decltype(e)是e命名的实体的类型。 If there is no such entity, or if e names a set of overloaded functions, the program is ill-formed; 如果没有这样的实体,或者如果e命名了一组重载函数,那么该程序就会形成错误;
  • otherwise, if e is a function call (5.2.2) or an invocation of an overloaded operator (parentheses around e are ignored), decltype(e) is the return type of the statically chosen function; 否则,如果e是函数调用(5.2.2)或重载操作符的调用(忽略e周围的括号),则decltype(e)是静态选择函数的返回类型;
  • otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e; 否则,如果e是左值,则decltype(e)是T&,其中T是e的类型;
  • otherwise, decltype(e) is the type of e. 否则,decltype(e)是e的类型。 The operand of the decltype specifier is an unevaluated operand (Clause 5). decltype说明符的操作数是未评估的操作数(第5条)。

decltype works by fetching the appropriate declaration and returning the desired type from it. decltype通过获取适当的声明并从中返回所需的类型来工作。 It has little intelligence with respect to non-overloaded operators. 它对于非重载运算符几乎没有智能。 Perhaps another point 也许是另一点

  • otherwise, if e is an xvalue, decltype(e) is T&& , where T is the type of e 否则,如果e是x值,则decltype(e)是T&& ,其中Te的类型

would be in order, particularly since, as written, xvalues get treated as prvalues. 将是有序的,特别是因为,如所写的,xvalues被视为prvalues。 Furthermore, your expression corresponds exactly to the definition of std::common_type (§20.7.6.6/3). 此外,您的表达式与std::common_type (第20.7.6.6 / 3节)的定义完全对应。

One straightforward workaround (to coin a phrase :vP ): 一个简单的解决方法(硬币短语:vP):

template< typename T1, typename T2 >
struct common_type_and_category {
    typedef typename std::conditional<
        std::is_same< T1, T2 >::value,
        T1,
        typename std::common_type< T1, T2 >::type
    >::type type;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM