decltype 应该如何与运算符一起使用,
decltype 应该产生其参数的类型。 逗号表达式应该具有其右侧操作数的类型。在下面的示例中,当使用 VS2010 编译时,除了 c2 之外的所有内容都是 false。当谈到 c1 时,这对我来说很奇怪,但显然符合标准,而对于 c4 和 c5 我不确定......这是正确的,还是编译器错误?遗憾的是,我的真实代码至少在 c4 中依赖 is_reference 返回 true。
函数的最后两行按预期编译,这表明逗号表达式的实际计算有效。
#include <type_traits>
void comma()
{
int str = 1;
bool c1 = std::is_reference<decltype(str)>::value;
bool c2 = std::is_reference<decltype((str))>::value;
bool c3 = std::is_reference<decltype((str, str))>::value;
bool c4 = std::is_reference<decltype((str, str))>::value;
bool c5 = std::is_reference<decltype((str, (str)))>::value;
int& s2 = (1, str);
s2 = 2;
}
decltype is supposed to yield the type of its parameter.
A comma expression is supposed to have the type of its right hand operand. In the example below all but c2 are false when compiled with VS2010. When it comes to c1 this is weird to me but clearly standard compliant, while for c4 and c5 I'm not sure... is this correct, or is it a compiler bug? Sadly enough, my real code relied on is_reference to return true in at least for c4.
Two last lines of function compiles as expected which shows that the actual evaluation of the comma expression works.
#include <type_traits>
void comma()
{
int str = 1;
bool c1 = std::is_reference<decltype(str)>::value;
bool c2 = std::is_reference<decltype((str))>::value;
bool c3 = std::is_reference<decltype((str, str))>::value;
bool c4 = std::is_reference<decltype((str, str))>::value;
bool c5 = std::is_reference<decltype((str, (str)))>::value;
int& s2 = (1, str);
s2 = 2;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,您是正确的
decltype((str, str))
和decltype((str, (str)))
都应该表示int&
作为具有逗号运算符的表达式,应与第二个操作数具有相同的值和值类别,并且str
是左值。我认为这是 VS2010 中 C++0x 支持的一个错误。
As far as I can see, you are correct that
decltype((str, str))
anddecltype((str, (str)))
should both denoteint&
as an expression that has a comma operator should have the same value and value category as the second operand andstr
is an lvalue.I think that it's a bug in C++0x support in VS2010.