任何人都可以解释为什么下面会编译
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/