KjellKod / g3log

G3log is an asynchronous, "crash safe", logger that is easy to use with default logging sinks or you can add your own. G3log is made with plain C++14 (C++11 support up to release 1.3.2) with no external libraries (except gtest used for unit tests). G3log is made to be cross-platform, currently running on OSX, Windows and several Linux distros. See Readme below for details of usage.
http://github.com/KjellKod/g3log
The Unlicense
908 stars 271 forks source link

constexpr is not applicable for pointers #495

Closed CryptoManiac closed 1 year ago

CryptoManiac commented 1 year ago

Here:

https://github.com/KjellKod/g3log/blob/bad9c58e608498e62ec321f59c78d86aa4ef9e8a/src/g3log/loglevels.hpp#L98

Initialization results with allocation of sub-object on heap. Pointers cannot be used as part of constant expressions.

constexpr should be replaced with const.

CryptoManiac commented 1 year ago
diff --git a/src/g3log/loglevels.hpp b/src/g3log/loglevels.hpp
index 1ddfc63..1d1bb4b 100644
--- a/src/g3log/loglevels.hpp
+++ b/src/g3log/loglevels.hpp
@@ -35,9 +35,6 @@ struct LEVELS {

    LEVELS(const LEVELS& other): value(other.value), text(other.text.c_str()) {}

-   #if __cplusplus >= 202002L
-      constexpr
-   #endif
    LEVELS(int id, const std::string& idtext) : value(id), text(idtext) {}

    bool operator==(const LEVELS& rhs)  const {
@@ -94,11 +91,7 @@ namespace g3 {
    static const int kInternalFatalValue = 2000;
 } // g3

-#if __cplusplus >= 202002L
-   constexpr
-#else
-   const
-#endif
+const
 LEVELS G3LOG_DEBUG{g3::kDebugValue, "DEBUG"},
    INFO {g3::kInfoValue, "INFO"},
    WARNING {g3::kWarningValue, "WARNING"},
@@ -137,11 +130,7 @@ namespace g3 {

 namespace g3 {
    namespace internal {
-      #if __cplusplus >= 202002L
-         constexpr
-      #else
-         const
-      #endif
+      const
       LEVELS CONTRACT {g3::kInternalFatalValue, {"CONTRACT"}},
             FATAL_SIGNAL {g3::kInternalFatalValue + 1, {"FATAL_SIGNAL"}},
             FATAL_EXCEPTION {kInternalFatalValue + 2, {"FATAL_EXCEPTION"}};
KjellKod commented 1 year ago

@SzGaa please take a look at this.

SzGaa commented 1 year ago

Hi,

@CryptoManiac thanks for the report, tomorrow I will verify the issue and check the standard to see if there is an easy way to fix it (other than revert to 'const', but totally agree with @KjellKod this is a syntax sugar, drop it asap if caused problems)

Sorry it went unnoticed on my side, should have seen with dynamic analysis tool.

Best Regards, Gábor

KjellKod commented 1 year ago

solved in https://github.com/KjellKod/g3log/pull/496/files