none
Ambiguity in Ternary operator vs if-else when upgrading from Visual Studio 10 to Visual Studio 13

    Question

  • 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;
    }

    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.

    Here is the code.

    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;
    }
    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 ?

    Friday, November 18, 2016 5:56 AM

All replies

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

    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.

    Thanks
    Dave

    Friday, November 18, 2016 8:27 AM
  • Hi vivek_bansal,

    According to your description, I create a sample Win32 Console application which uses Expression?Value1:Value2 in my Visual Studio 2013, which is working fine in my side. Please add following code into your Visual Studio 2013 in a new project to check whether it works or not.

    int _tmain(int argc, _TCHAR* argv[])
    {
    	bool upper = false;
    	int i = 1;
    
    	i += (upper) ? 2 : 3;
    
    	return 0;
    }

    If above code has the same error in new project, I suggest you check whether your Visual Studio 2013 has installed the latest Update. And please run your Visual Studio 2013 in safe mode prevents all third-party VSPackages from loading when Visual Studio starts, thus ensuring stable execution.

    If above code working fine in new project, I'm afraid that there has any problem in your project when it upgrade from VS2010 to VS2013. I suggest you create a new project in Visual Studio 2013 and then move the project files into new project one by one. It could help us confirm which file causes this issue.

    If above steps could not find out what happened, please provide a sample project as David suggested, which could help us reproduce this issue in our side.

    Best Regards,
    Weiwei


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 21, 2016 8:03 AM
    Moderator