decltype(void())和decltype(void {})之间的差异 [英] Differences between decltype(void()) and decltype(void{})

查看:303
本文介绍了decltype(void())和decltype(void {})之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是此问题的后续问题: 中的 c(c)>
$ b




decltype(void())编译良好, void()意味着在这种情况下在上面提到的问题(实际上在答案中)中解释。

另一方面,我注意到 decltype (void {})不编译。



它们之间有什么区别(在 至少)?

为什么不编译第二个表达式?






为了完整性,它遵循一个最小(not-)工作示例:

  int main(){
// this does not compile
// decltype(void {})* ptr = nullptr;
//这个编译好
decltype(void())* ptr = nullptr;
(void)ptr;
}


解决方案

sizeof 一起使用时,void()被解释为type-id。

void与 decltype 一起使用时,被解释为一个表达式。



我认为 void {} 在任何上下文中都有效。它既不是有效的类型ID也不是有效的表达式。


This is a follow-up of the question: What does the void() in decltype(void()) mean exactly?.


decltype(void()) compiles fine and what the void() means in this case is explained in the above mentioned question (actually in the answer).
On the other side, I noticed that decltype(void{}) doesn't compile.

What's the difference between them (in the context of a decltype at least)?
Why doesn't the second expression compile?


For completeness, it follows a minimal (not-)working example:

int main() {
    // this doesn't compile
    //decltype(void{}) *ptr = nullptr;
    // this compiles fine
    decltype(void()) *ptr = nullptr;
    (void)ptr;
}

解决方案

void() is interpreted as type-id when used with sizeof.
void() is interpreted as an expression when used with decltype.

I don't think void{} is valid in any context. It is neither a valid type-id nor a valid expression.

这篇关于decltype(void())和decltype(void {})之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆