Writing Portable Code in C++’�Variable Scope

Environment: C++

Writing portable code is an interesting as well as a challenging task sometimes. You have to restrict yourself not only to the standard of the language but sometimes also to the implementation of the language and its libraries on different platforms. Sometimes the standard of language doesn’t say anything about the implementation of some features and lets the vendor implement it in their own way. The most suitable example of this that comes to mind is the sort algorithm of the standard library. The C++ standard [ISO98] doesn’t say anything about which algorithm should be used for sorting; therefore, vendors are free to use any sort function that fulfills the requirements of the standard.

Other problems that may come during the writing of portable code are different levels of compliance with the standards of different compilers. Suppose you are working in an environment in which your team uses different compilers to compile a project. Sometimes it becomes tricky, if not impossible, to write code that can run on all compilers. The ideal solution of this problem is to use the same compiler, which is as compliant with the standard as possible. But, in the real world, it is not possible due to lots of factors such as personal preference, license issues, productivity, optimization, and so forth. One example of such a problem is the scope of variables in a loop.

According to the C++ standard, the scope of the variable that declares inside the “for” loop is limited to the loop; in other words, this code is legal according to the C++ standard, but some compilers, such as Visual C++ 6, can’t compile it successfully.


const int iMax = 100;
for (int iIndex = 0; iIndex < iMax; ++iIndex)
{
// do something
}

for (int iIndex = 0; iIndex < iMax; ++iIndex)
{
// do something
}

And, if we remove the declaration of the variable inside the “for” loop, this code not only becomes illegal but also can’t compile on standard C++ compilers such as Comeau C/C++.


const int iMax = 10;
for (int iIndex = 0; iIndex < iMax; ++iIndex)
{
// do something
}

for (iIndex = 0; iIndex < iMax; ++iIndex)
{
// do something
}

The easiest solution yo this problem is to put the variable declaration outside the scope of the for loop to make all compilers happy.


const int iMax = 10;
int iIndex;

for (iIndex = 0; iIndex < iMax; ++iIndex)
{
// do something
}

for (iIndex = 0; iIndex < iMax; ++iIndex)
{
// do something
}

There is one problem with this technique—the scope of the iIndex variable. Now, this variable becomes visible on that part of code where it shouldn’t. There is an interesting technique discussed in [DEV02] to limit the scope as well as make both compilers happy. The technique is to just put extra braces around the “for” loop to limit its scope visible inside the loop.


const int iMax = 10;
{for (int iIndex = 0; iIndex < iMax; ++iIndex)
{
// do something
}}

{for (int iIndex = 0; iIndex < iMax; ++iIndex)
{
// do something
}}

These are a few reasons why writing portable code is challenging as well as fun. So, be sure to check your code on different implementations on different platform if you want to make it portable, even if it is written according to a language standard, because different implementations may behave differently on your code although they follow the rules of a standard language.

See Writing Portable Code in C++—A Vectors and String Issue for more issues regarding the writing of portable code.

References


  1. [DEW02] C++ Gotchas, Avoiding Common Problems in Coding and Design. Stephen Dewhurst 2002

  2. [ISO98] International Standard Programming Language C++ ISO/ICE 14882 1998

  3. [JOS99] The C++ Standard Library: A Tutorial and Reference. Nicolai M. Josuttis 1999

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read