c++ - 不同的编译结果 : Mac vs Windows

标签 c++ xcode macos visual-c++ stl

全部,

我正在从事涉及大量使用 STL 及其跨平台的项目。在 Windows 上,我使用的是 MSVC 2010 Pro,而在 Mac 端,我在 Snow Leopard 上安装了 XCode 4.2。

我的代码是这样的:

m_sort.m_type.size() == 0 ? m_sort.m_type.push_back( SortObject( SORT_BY_NAME, true ) ) : m_sort.m_type.insert( it - 1, SortObject( SORT_BY_NAME, true ) );

其中 m_sort.m_type 是 std::vector<>,它将用于对另一个 std::vector<> 进行排序。

Visual Studio 可以很好地编译此代码:没有警告,没有错误。 但是,尝试在 XCode 上编译此代码时出现此错误:

Left operand to ? is void, but right operand is of type 'iterator' (aka '__normal_iterator')

在 Mac 上有解决错误的简单方法吗? 为什么这段代码在 Windows 上构建成功? 或者可能是 XCode for SL 与 MSVC 在 Windows 上的 STL 实现不同?

谢谢。

最佳答案

写成下面的方式

m_sort.m_type.size() == 0 ? 
   m_sort.m_type.push_back( SortObject( SORT_BY_NAME, true ) ) : 
   ( void )m_sort.m_type.insert( it - 1, SortObject( SORT_BY_NAME, true ) );

来自 C++ 标准

2 If either the second or the third operand has type void, one of the following shall hold: — The second or the third operand (but not both) is a (possibly parenthesized) throw-expression (15.1); the result is of the type and value category of the other.

— Both the second and the third operands have type void; the result is of type void and is a prvalue. [ Note: This includes the case where both operands are throw-expressions. —end note ]

关于c++ - 不同的编译结果 : Mac vs Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21616693/

相关文章:

C++函数指针错误

ios - 如何使用 BitBucket 配置 Xcode

macos - Applescript for Outlook - 将邮件标记为已读

c++ - 合并应用程序资源 - mac

c++ - 在opencv中发现凸性缺陷? [崩溃取决于给定的输入图像..]

ios - 在 UITextView 上以编程方式禁用 iOS8 Quicktype 键盘

ios - 将文件包放入iOS应用程序中的特定文件夹位置

macos - 无法在沙盒 cocoa 应用程序中执行 shell 脚本命令

cocoa - 基于 Cocoa 文档的应用程序中文档的默认保存位置

c++ - c 不匹配 if 的 else 是非法的