从Visual Studio 10升级到Visual Studio 13时,三元运算符中的歧义与if-else [英] Ambiguity in Ternary operator vs if-else when upgrading from Visual Studio 10 to Visual Studio 13

查看:79
本文介绍了从Visual Studio 10升级到Visual Studio 13时,三元运算符中的歧义与if-else的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我将代码从Visual Studio 10升级到了Visual Studio 13,这导致我们的代码中断.

Recently I upgraded our code from Visual Studio 10 to Visual Studio 13 which is leading to a break in our code.

当我调试代码时,我发现了导致它的代码(将数字转换为罗马数字)

When I debug the code I found out the code(converting numeric number to Roman) which is causing it:

MakeRoman(StringT s, IntT n, BoolT upper)
{
	register StringT cp1, cp2;
	register IntT i, j;
	if (n <= 0)
	{
		*s++ = '0';
		*s = 0;
		return;
	}
	n = min(9999, n);
	cp1 = s;
	*cp1 = 0;
	for (i = 0; i < ROWS; i++)
		for (j = 0; j < COLS; j++)
		{
			while (n >= arabic[i][j])
			{
				for (cp2 = (StringT) roman[i][j]; *cp2 && cp1 - s < MAX_NUMLEN; cp2++)
					*cp1++ = (upper) ? (CharToUpperRomanOnly(*cp2)) : (*cp2);  //reason for break
				n -= arabic[i][j];
			}
		}
	*cp1 = 0;
}

以上突出显示的三元运算符引起中断(将24转换为xiv而不是xxiv).当我将其转换为if-else构造时,它工作正常.

Above highlighted ternary operator is causing the break (converting 24 to xiv instead of xxiv). When I convert it to if-else construct it is working fine.

这是代码.

MakeRoman(StringT s, IntT n, BoolT upper)
{
	register StringT cp1, cp2;
	register IntT i, j;
	if (n <= 0)
	{
		*s++ = '0';
		*s = 0;
		return;
	}

	n = min(9999, n);
	cp1 = s;
	*cp1 = 0;
	for (i = 0; i < ROWS; i++)
		for (j = 0; j < COLS; j++)
		{
			while (n >= arabic[i][j])
			{
				for (cp2 = (StringT)roman[i][j]; *cp2 && cp1 - s < MAX_NUMLEN; cp2++)
				{ // Now working fine in VS 13
					if (upper)
						*cp1 = CharToUpperRomanOnly(*cp2);
					else
						*cp1 = *cp2;
					cp1++;

				}
				n -= arabic[i][j];
			}
		}
	*cp1 = 0;
}

为什么VS13编译器显示这种类型的歧义行为,以及为什么它在VS 10中能正常工作?编译器在这里进行任何优化吗?

Why VS13 compiler is showing this type of ambiguous behaviour and why it is working perfectly in VS 10 ? Are there any type of optimisations which compiler is doing here ?

推荐答案

>最近我将代码从Visual Studio 10升级到了Visual Studio 13,这导致了我们的突破代码.

>Recently I upgraded our code from Visual Studio 10 to Visual Studio 13 which is leading to a break in our code.

您能否将代码打包到一个简单的控制台项目/解决方案中
重现问题,以便任何人都可以轻松尝试重现
问题.如果您可以将其发布到onedrive(或其他公共云)上
存储)共享,这将使事情变得简单.

Could you package up your code into a simple console project/solution
that reproduces the problem so that anyone can easily try to reproduce
the problem. If you can post it on a onedrive (or other public cloud
storage) share, that'll make things easy.

谢谢
戴夫


这篇关于从Visual Studio 10升级到Visual Studio 13时,三元运算符中的歧义与if-else的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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