c++ - 如何从 switch 语句中排除浮点值?

标签 c++ visual-studio

在考试学习指南上,我的老师告诉我们,我们需要知道如何编写 switch 语句,但它必须排除浮点值。在我们的学习小组中,我们玩弄了使用“自动”的想法,但我们不确定如何实现他所要求的。有什么建议吗?

最佳答案

switch statement 条件中使用的表达式必须是:

any expression of integral or enumeration type, or of a class type contextually implicitly convertible to an integral or enumeration type, or a declaration of a single non-array variable of such type with a brace-or-equals initializer.

浮点类型与其中任何一个都不匹配。

你说:

but it must exclude float values.

您无需执行任何操作来排除在 switch 语句的条件中使用浮点表达式。

如果您尝试使用,您的编译器应该报告错误:

double x;
std::cin >> x;
switch (x)
{
  ... 
}

关于c++ - 如何从 switch 语句中排除浮点值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49182096/

相关文章:

c# - 如何附加调试器以从托管(C#)包装器进入 native (C++)代码?

c++ - “g++”在 Windows 8 上未被识别为内部或外部命令

c++ - Visual C++ std::regex_search 错误?

c++ - 在调用点参数处创建的临时对象的生命范围

c++ - 网络之间的套接字连接超时有所不同

c# - 无法将图片从 picturebox 保存到 datagridview

c# - 我怎样才能设置 CodeRush 和 Visual Assist X 不相互冲突?

c# - 在 Visual Studio 调试器中查看 Lambda

.net - 是否有用于以编程方式更改项目和解决方案文件(.vbproj、.csproj、.sln)的 API?

c++ - libzip 无法关闭文件