为什么不使用std :: max和c ++ / CLI编译? [英] why doesn't this compile when using std::max and c++/CLI?

查看:182
本文介绍了为什么不使用std :: max和c ++ / CLI编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can anyone please explain why the following will compile

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?

wont?它给出的错误是错误C2665:'std :: max':没有7重载可以转换所有的参数类型

wont? The error it gives is error C2665: 'std::max' : none of the 7 overloads could convert all the argument types

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

The variable aAssigmments is of type array<List<int>^>^ and fInstanceData->NumRequiredEmpsPerJob is of type array<int>^

std :: max 的手册声明它通过引用获取值,因此它显然是在第一个示例中隐式地这样做,所以为什么编译器不会这样做对于count属性返回的整数值,如第二个示例中所示?

The manual for std::max states that it takes values by reference, so it's clearly doing this implicitly in the first example, so why can't the compiler do the same for integer values returned by the count property, as in the second example? Can I get a reference to an int explicitly?

推荐答案

(int)(aAssignments [i] - > Count)将调用属性getter。

(int)(aAssignments[i]->Count) will call the property getter. But it evaluates to a temporary variable (rvalue) which cannot bind to a non-const reference.

根据我对 std :: max的文档,我们可以看到,

According to my documentation on std::max, the parameters should be const references and everything should work.

如果您明确指定了模板类型参数,会出现什么情况,例如

What happens if you explicitly specify the template type parameter, e.g.

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

max< int& (a + 0,b + 0)

这篇关于为什么不使用std :: max和c ++ / CLI编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆