types - 为什么在使用 std::max 和 c++/CLI 时不能编译?

标签 types c++-cli max

任何人都可以解释为什么下面会编译

int a = aAssignments[i]->Count;
int b = fInstanceData->NumRequiredEmpsPerJob[i];
fInstanceData->NumSlotsPerJob[i] = max(a,b);

但是

fInstanceData->NumSlotsPerJob[i] = max((int)(aAssignments[i]->Count), (int)(fInstanceData->NumRequiredEmpsPerJob[i])); //why on earth does this not work?

不会吗?它给出的错误是 error C2665: 'std::max' : none of the 7 overloads could convert all the argument types

变量aAssigmments类型为 array<List<int>^>^fInstanceData->NumRequiredEmpsPerJob类型为 array<int>^

std::max 的手册声明它通过引用获取值,因此在第一个示例中显然是隐式执行此操作,那么为什么编译器不能像第二个示例那样对 count 属性返回的整数值执行相同的操作?我可以显式获取对 int 的引用吗?

最佳答案

(int)(aAssignments[i]->Count)将调用属性 getter 。但它求值为一个不能绑定(bind)到非常量引用的临时变量(右值)。

根据我关于 std::max 的文档,参数应该是常量引用,一切都应该有效。

如果您显式指定模板类型参数会发生什么,例如

max<int>((int)(aAssignments[i]->Count), (int)(fInstanceData->NumRequiredEmpsPerJob[i]))

?

max<int>(a + 0, b + 0)呢? ?

关于types - 为什么在使用 std::max 和 c++/CLI 时不能编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3986522/

相关文章:

typescript - 箭头样式用户定义的类型防护?

德尔福数学: Why is 0. 7<0.70?

c# - 错误 LNK2028 : Thrown when instantiating an native C++ class within C++/CLI

c# - 如何在linq中选择最大的字符串到nhibernate

sql - 避免在SQL Server 2008中使用MAX + 1整数进行并发问题…制作自己的IDENTITY值

java - 特定类型会影响 ArrayList 的性能吗?

Haskell 的 Pairs 类型

visual-c++ - Visual C++ 非托管和托管

.net - 内存泄漏C++/CLI方法

sql - 使用 3 个表选择最大数据