gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:49:01 25 4
gpt4 key购买 nike

全部,

我正在从事涉及大量使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com