c++ - 构造一个 void 类型?

标签 c++ void

给了我一段使用 void() 的代码作为论据。代码无法编译……显然?

我们可以实例化 void 类型的任何东西吗? ?我相信答案是否定的,除了 void* .例如:

  1. 编写函数void askVoid(void param) {}错误:

A parameter may not have void type

  1. 编写函数void askNaught() {}并用 askNaught(void())` 错误调用它:

error C2660: takeNaught: function does not take 1 arguments

  1. 编写模板化函数 template <typename T> void takeGeneric(T param) {}并用 takeGeneric(void()) 调用它错误:

error C2893: Failed to specialize function template void takeGeneric(T)

  1. 声明 void voidType错误:

Incomplete type is not allowed

  1. 声明 auto autoVoid = void()错误:

Cannot deduce auto type

  1. 声明 void* voidPtr工作正常,但 remove_pointer_t<decltype(voidPtr)> decltypeVoid错误:

error C2182: decltypeVoid: illegal use of type void

就是这样,对吧?没有void()的地方在 C++ 中有吗?这只是我得到的错误代码,对吧?

最佳答案

C++(我说的是 C++,而不是 C)允许 (§6.6.3 comma 2 ) 具有 void 返回类型的函数返回一个 void 表达式,即:

void foo() { return void(); }

但请注意,它不是构造一个临时的void!

关于c++ - 构造一个 void 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37701321/

相关文章:

c++ - 最简单的 Gtk::Builder get_widget() 程序出错

c++ - 我可以包含全局静态成员吗​​?

java - Java中如何使用void中的变量来声明另一个变量?

c - 返回平均值、最大值、最小值的 Void 函数,并以数组和输入数量作为参数

java - 使用带有两个 int 参数而不是 int 的 void 方法?

perl - 为什么 Perl 会提示 "Useless use of a constant in void context",但只是有时?

c++ - Windows 是进入休眠模式还是使用 C++ 休眠?

C++ Win32 控制台颜色

c++ - round 比 LLVM 中的 floor/ceil/int 慢得多

c - 有没有办法将 void *buf 转换为 char?