TestForANSIForScope
This module checks whether the CXX compiler restricts the scope of variables declared in a for-init-statement to the loop body.
Load this module in a CMake project with:
include(TestForANSIForScope)
In early C++ (pre-C++98), variables declared in for(<init-statement> ...) could remain accessible outside the loop after its body (for() { <body> }).
This module defines the following cache variable:
-
CMAKE_NO_ANSI_FOR_SCOPE -
A cache variable containing the result of the check. It will be set to value
0if the for-init-statement has restricted scope (C++ 98and newer), and to value1if not (ANSI C++).
Note
As of the C++ 98 standard, variables declared in a for-init-statement are restricted to the loop body, making this behavior obsolete.
Examples
Including this module will check the for() loop scope behavior and define the CMAKE_NO_ANSI_FOR_SCOPE cache variable:
CMakeLists.txtinclude(TestForANSIForScope) file( CONFIGURE OUTPUT config.h CONTENT "#cmakedefine CMAKE_NO_ANSI_FOR_SCOPE" )
which can be then used in a C++ program:
example.cxx#include "config.h"
#ifdef CMAKE_NO_ANSI_FOR_SCOPE
# define for if(false) {} else for
#endif
See Also
- The
CMakeBackwardCompatibilityCXXmodule.
© 2000–2026 Kitware, Inc. and Contributors
Licensed under the BSD 3-clause License.
https://cmake.org/cmake/help/latest/module/TestForANSIForScope.html