]> OCCT Git - occt.git/commitdiff
Coding - Create a C++17 version macro (#785)
authorPasukhin Dmitry <dpasukhi@opencascade.com>
Fri, 31 Oct 2025 08:45:06 +0000 (08:45 +0000)
committerGitHub <noreply@github.com>
Fri, 31 Oct 2025 08:45:06 +0000 (08:45 +0000)
- Updated preprocessor directives to utilize Standard_CPP17_OR_HIGHER instead of direct __cplusplus checks.
- Removed unnecessary preprocessor checks for C++17 in TCollection_AsciiString implementation and tests.
- Enhanced code readability and maintainability by centralizing C++ version checks.

src/FoundationClasses/TKernel/GTests/TCollection_AsciiString_Test.cxx
src/FoundationClasses/TKernel/Standard/Standard_Macro.hxx
src/FoundationClasses/TKernel/TCollection/TCollection_AsciiString.hxx
src/FoundationClasses/TKernel/TCollection/TCollection_AsciiString.lxx

index fe3c72c6927e09a30398487caee3397603ad0216..64715e1257a36587a2a5da46529911731fee6e8e 100644 (file)
@@ -1207,7 +1207,6 @@ TEST(TCollection_AsciiStringTest, Trunc_NoChange)
 // Tests for std::string_view API
 // ========================================
 
-#if defined(__cplusplus) && __cplusplus >= 201703L
 TEST(TCollection_AsciiStringTest, StringView_Constructor)
 {
   std::string_view        aView("Hello World");
@@ -1254,7 +1253,6 @@ TEST(TCollection_AsciiStringTest, StringView_Insert)
   aString.Insert(2, aView);
   EXPECT_STREQ("HelloWorld", aString.ToCString());
 }
-#endif
 
 // ========================================
 // Tests for Move semantics
index 2daf95d045ff421d82349dbb2585cc29d9c82f16..8c8b5e07a42b451da962ce9a982f0af3141d293b 100644 (file)
   #endif
 #endif
 
+//! @def Standard_CPP17_OR_HIGHER
+//! Macro to check if C++ standard version is C++17 or higher.
+//!
+//! Expands to 1 if C++17 or higher is available, 0 otherwise.
+//! Uses _MSVC_LANG for MSVC (available since VS 2017.3) which correctly
+//! reports the language standard regardless of /Zc:__cplusplus flag.
+//! For other compilers or as fallback, uses __cplusplus standard macro.
+#if defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 201703L
+  // MSVC: _MSVC_LANG correctly reports language standard (available since VS 2017.3)
+  #define Standard_CPP17_OR_HIGHER 1
+#elif defined(__cplusplus) && __cplusplus >= 201703L
+  // Standard __cplusplus check for GCC, Clang, and MSVC when _MSVC_LANG is not available
+  #define Standard_CPP17_OR_HIGHER 1
+#else
+  #define Standard_CPP17_OR_HIGHER 0
+#endif
+
 #endif
index b2df46f5cc90b0b53a6be02daa858e26cc6c6197..2f6f00ee22acc691f2712504b9c567f4945cb8c9 100644 (file)
@@ -28,7 +28,7 @@
 #include <Standard_IStream.hxx>
 #include <Standard_Macro.hxx>
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   #include <string_view>
 #endif
 
@@ -57,7 +57,7 @@ public:
   //! Initializes a AsciiString to an empty AsciiString.
   Standard_EXPORT TCollection_AsciiString();
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Initializes a AsciiString with a string_view.
   //! @param[in] theStringView the string view to initialize from
   explicit inline TCollection_AsciiString(const std::string_view& theStringView);
@@ -190,7 +190,7 @@ public:
 
   void operator+=(const Standard_CString theCString) { AssignCat(theCString); }
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Appends string view to this ASCII string. This is an unary operator.
   //! @param[in] theStringView the string view to append
   inline void AssignCat(const std::string_view& theStringView);
@@ -303,7 +303,7 @@ public:
 
   inline TCollection_AsciiString operator+(const Standard_CString theCString) const;
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Appends string view to this ASCII string.
   //! @param[in] theStringView the string view to append
   //! @return new string with string view appended
@@ -384,7 +384,7 @@ public:
 
   void operator=(const Standard_CString theCString) { Copy(theCString); }
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Copy string view to this ASCII string.
   //! Used as operator =
   //! @param[in] theStringView the string view to copy from
@@ -477,7 +477,7 @@ public:
                                              const Standard_Integer         theFromIndex,
                                              const Standard_Integer         theToIndex) const;
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Returns the index of the first character of this string that is present in string_view.
   //! @param[in] theSet the string view of characters to search for
   //! @param[in] theFromIndex the starting index for search
@@ -535,7 +535,7 @@ public:
                                                 const Standard_Integer         theFromIndex,
                                                 const Standard_Integer         theToIndex) const;
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Returns the index of the first character of this string that is not present in string_view.
   //! @param[in] theSet the string view of characters to check against
   //! @param[in] theFromIndex the starting index for search
@@ -591,7 +591,7 @@ public:
   //! @param[in] theCString the C string to insert
   inline void Insert(const Standard_Integer theWhere, const Standard_CString theCString);
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Inserts a string_view at position theWhere.
   //! @param[in] theWhere position to insert at
   //! @param[in] theStringView the string view to insert
@@ -634,7 +634,7 @@ public:
   //! @param[in] theCString the C string to insert
   inline void InsertAfter(const Standard_Integer theIndex, const Standard_CString theCString);
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Inserts a string_view after a specific index in this string.
   //! Raises an exception if index is out of bounds.
   //! @param[in] theIndex the index to insert after
@@ -671,7 +671,7 @@ public:
   //! @param[in] theCString the C string to insert
   inline void InsertBefore(const Standard_Integer theIndex, const Standard_CString theCString);
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Inserts a string_view before a specific index in this string.
   //! Raises an exception if index is out of bounds.
   //! @param[in] theIndex the index to insert before
@@ -713,7 +713,7 @@ public:
 
   inline Standard_Boolean operator==(const Standard_CString theCString) const;
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Returns true if the characters in this ASCII string
   //! are identical to the characters in string_view.
   //! @param[in] theStringView the string view to compare with
@@ -765,7 +765,7 @@ public:
 
   inline Standard_Boolean operator!=(const Standard_CString theCString) const;
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Returns true if there are differences between the
   //! characters in this ASCII string and string_view.
   //! @param[in] theStringView the string view to compare with
@@ -807,7 +807,7 @@ public:
 
   Standard_Boolean operator<(const Standard_CString theCString) const { return IsLess(theCString); }
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Returns TRUE if this ASCII string is lexicographically less than theStringView.
   //! @param[in] theStringView the string view to compare with
   //! @return true if this string is lexicographically less than theStringView
@@ -848,7 +848,7 @@ public:
 
   inline Standard_Boolean operator>(const Standard_CString theCString) const;
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Returns TRUE if this ASCII string is lexicographically greater than theStringView.
   //! @param[in] theStringView the string view to compare with
   //! @return true if this string is lexicographically greater than theStringView
@@ -884,7 +884,7 @@ public:
   //! @return true if this string starts with theCString
   inline Standard_Boolean StartsWith(const Standard_CString theCString) const;
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Determines whether the beginning of this string instance matches the specified string_view.
   //! @param[in] theStartString the string view to check for at the beginning
   //! @return true if this string starts with theStartString
@@ -904,7 +904,7 @@ public:
   //! @return true if this string ends with theEndString
   inline Standard_Boolean EndsWith(const TCollection_AsciiString& theEndString) const;
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Determines whether the end of this string instance matches the specified string_view.
   //! @param[in] theEndString the string view to check for at the end
   //! @return true if this string ends with theEndString
@@ -1158,7 +1158,7 @@ public:
   //! @return the position of first match, or -1 if not found
   inline Standard_Integer Search(const Standard_CString theCString) const;
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Searches a string_view in this string from the beginning
   //! and returns position of first item matching.
   //! It returns -1 if not found.
@@ -1194,7 +1194,7 @@ public:
   //! @return the position of first match from end, or -1 if not found
   inline Standard_Integer SearchFromEnd(const Standard_CString theCString) const;
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Searches a string_view in this string from the end
   //! and returns position of first item matching.
   //! It returns -1 if not found.
@@ -1242,7 +1242,7 @@ public:
   //! @param[in] theCString the C string to replace with
   inline void SetValue(const Standard_Integer theWhere, const Standard_CString theCString);
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Replaces a part of this ASCII string with a string_view.
   //! @param[in] theWhere position to start replacement
   //! @param[in] theStringView the string view to replace with
@@ -1284,7 +1284,7 @@ public:
   //! @return the C string representation
   Standard_CString ToCString() const { return myString; }
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Returns string_view for this AsciiString.
   //! This provides a lightweight, non-owning view of the string data.
   //! @return the string_view representation
@@ -1378,7 +1378,7 @@ public:
   static Standard_Boolean IsEqual(const TCollection_AsciiString& string1,
                                   const Standard_CString         string2);
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Returns True when the ASCII string and string_view are the same.
   //! (Just for HashCode for AsciiString)
   //! @param[in] theString1 first string to compare
@@ -1438,7 +1438,7 @@ public:
                                               const TCollection_AsciiString& theString2,
                                               const Standard_Boolean         theIsCaseSensitive);
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Returns True if the string and string_view contain same characters.
   //! @param[in] theString1 first string to compare
   //! @param[in] theStringView second string view to compare
@@ -1467,7 +1467,7 @@ public:
                                               const Standard_CString theCString2,
                                               const Standard_Boolean theIsCaseSensitive);
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
   //! Returns True if the two string_views contain same characters.
   //! @param[in] theStringView1 first string view to compare
   //! @param[in] theStringView2 second string view to compare
index 20d0f1709cb88741dd4bad6e9a11e962d3477e03..037b8d7c69d9276aacf5d2ba553290ea81085363 100644 (file)
@@ -52,7 +52,7 @@ inline void TCollection_AsciiString::AssignCat(const Standard_CString theCString
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline void TCollection_AsciiString::AssignCat(const std::string_view& theStringView)
 {
   if (!theStringView.empty())
@@ -88,7 +88,7 @@ inline TCollection_AsciiString TCollection_AsciiString::Cat(const Standard_CStri
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline TCollection_AsciiString TCollection_AsciiString::Cat(
   const std::string_view& theStringView) const
 {
@@ -113,7 +113,7 @@ inline void TCollection_AsciiString::Copy(const Standard_CString theCString)
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline void TCollection_AsciiString::Copy(const std::string_view& theStringView)
 {
   Copy(theStringView.data(), static_cast<Standard_Integer>(theStringView.size()));
@@ -159,7 +159,7 @@ inline void TCollection_AsciiString::Insert(const Standard_Integer theWhere,
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline void TCollection_AsciiString::Insert(const Standard_Integer  theWhere,
                                             const std::string_view& theStringView)
 {
@@ -195,7 +195,7 @@ inline void TCollection_AsciiString::InsertAfter(const Standard_Integer theIndex
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline void TCollection_AsciiString::InsertAfter(const Standard_Integer  theIndex,
                                                  const std::string_view& theStringView)
 {
@@ -231,7 +231,7 @@ inline void TCollection_AsciiString::InsertBefore(const Standard_Integer theInde
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline void TCollection_AsciiString::InsertBefore(const Standard_Integer  theIndex,
                                                   const std::string_view& theStringView)
 {
@@ -266,7 +266,7 @@ inline Standard_Boolean TCollection_AsciiString::IsEqual(const Standard_CString
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Boolean TCollection_AsciiString::IsEqual(
   const std::string_view& theStringView) const
 {
@@ -300,7 +300,7 @@ inline Standard_Boolean TCollection_AsciiString::IsDifferent(
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Boolean TCollection_AsciiString::IsDifferent(
   const std::string_view& theStringView) const
 {
@@ -334,7 +334,7 @@ inline Standard_Boolean TCollection_AsciiString::IsLess(const Standard_CString t
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Boolean TCollection_AsciiString::IsLess(const std::string_view& theStringView) const
 {
   return IsLess(theStringView.data(), static_cast<Standard_Integer>(theStringView.size()));
@@ -367,7 +367,7 @@ inline Standard_Boolean TCollection_AsciiString::IsGreater(const Standard_CStrin
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Boolean TCollection_AsciiString::IsGreater(
   const std::string_view& theStringView) const
 {
@@ -401,7 +401,7 @@ inline Standard_Boolean TCollection_AsciiString::StartsWith(const Standard_CStri
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Boolean TCollection_AsciiString::StartsWith(
   const std::string_view& theStartString) const
 {
@@ -419,7 +419,7 @@ inline Standard_Boolean TCollection_AsciiString::EndsWith(
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Boolean TCollection_AsciiString::EndsWith(
   const std::string_view& theEndString) const
 {
@@ -460,7 +460,7 @@ inline Standard_Integer TCollection_AsciiString::Search(const Standard_CString t
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Integer TCollection_AsciiString::Search(const std::string_view& theWhat) const
 {
   return Search(theWhat.data(), static_cast<Standard_Integer>(theWhat.size()));
@@ -494,7 +494,7 @@ inline Standard_Integer TCollection_AsciiString::SearchFromEnd(
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Integer TCollection_AsciiString::SearchFromEnd(
   const std::string_view& theWhat) const
 {
@@ -535,7 +535,7 @@ inline void TCollection_AsciiString::SetValue(const Standard_Integer theWhere,
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline void TCollection_AsciiString::SetValue(const Standard_Integer  theWhere,
                                               const std::string_view& theStringView)
 {
@@ -564,7 +564,7 @@ inline Standard_Boolean TCollection_AsciiString::IsEqual(const TCollection_Ascii
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Boolean TCollection_AsciiString::IsEqual(const TCollection_AsciiString& theString1,
                                                          const std::string_view& theStringView)
 {
@@ -574,7 +574,7 @@ inline Standard_Boolean TCollection_AsciiString::IsEqual(const TCollection_Ascii
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Boolean TCollection_AsciiString::IsEqual(const std::string_view& theStringView,
                                                          const TCollection_AsciiString& theString2)
 {
@@ -625,7 +625,7 @@ inline Standard_Boolean TCollection_AsciiString::IsSameString(
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Boolean TCollection_AsciiString::IsSameString(
   const TCollection_AsciiString& theString1,
   const std::string_view&        theStringView,
@@ -641,7 +641,7 @@ inline Standard_Boolean TCollection_AsciiString::IsSameString(
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Boolean TCollection_AsciiString::IsSameString(
   const std::string_view&        theStringView,
   const TCollection_AsciiString& theString2,
@@ -673,7 +673,7 @@ inline Standard_Boolean TCollection_AsciiString::IsSameString(
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Boolean TCollection_AsciiString::IsSameString(
   const std::string_view& theStringView1,
   const std::string_view& theStringView2,
@@ -763,7 +763,7 @@ inline Standard_Integer TCollection_AsciiString::FirstLocationInSet(
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Integer TCollection_AsciiString::FirstLocationInSet(
   const std::string_view& theSet,
   const Standard_Integer  theFromIndex,
@@ -802,7 +802,7 @@ inline Standard_Integer TCollection_AsciiString::FirstLocationNotInSet(
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Integer TCollection_AsciiString::FirstLocationNotInSet(
   const std::string_view& theSet,
   const Standard_Integer  theFromIndex,
@@ -855,7 +855,7 @@ inline TCollection_AsciiString TCollection_AsciiString::operator+(
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline TCollection_AsciiString TCollection_AsciiString::operator+(
   const std::string_view& theStringView) const
 {
@@ -898,7 +898,7 @@ inline Standard_Boolean TCollection_AsciiString::operator==(const Standard_CStri
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Boolean TCollection_AsciiString::operator==(
   const std::string_view& theStringView) const
 {
@@ -923,7 +923,7 @@ inline Standard_Boolean TCollection_AsciiString::operator!=(const Standard_CStri
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Boolean TCollection_AsciiString::operator!=(
   const std::string_view& theStringView) const
 {
@@ -941,7 +941,7 @@ inline Standard_Boolean TCollection_AsciiString::operator<(
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Boolean TCollection_AsciiString::operator<(
   const std::string_view& theStringView) const
 {
@@ -966,7 +966,7 @@ inline Standard_Boolean TCollection_AsciiString::operator>(const Standard_CStrin
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline Standard_Boolean TCollection_AsciiString::operator>(
   const std::string_view& theStringView) const
 {
@@ -976,7 +976,7 @@ inline Standard_Boolean TCollection_AsciiString::operator>(
 
 //=================================================================================================
 
-#if __cplusplus >= 201703L
+#if Standard_CPP17_OR_HIGHER
 inline TCollection_AsciiString::TCollection_AsciiString(const std::string_view& theStringView)
     : TCollection_AsciiString(theStringView.data(),
                               static_cast<Standard_Integer>(theStringView.size()))