Skip to content

Commit 8a813f1

Browse files
committed
Fix build for modern C++17 and Clang 17
Modernize the project to build with contemporary toolchains: - Upgrade C++ standard from C++11 to C++17 - Update CMake minimum version to 3.16 - Fix logger comparator const-correctness for modern libc++ - Resolve namespace/class name ambiguity using inline variables - Use typedef alias to disambiguate out-of-line member definitions The dotenv class name matches its namespace, which causes parsing issues in modern C++. Fixed by using C++17 inline variables for static members and a typedef alias in the implementation.
1 parent 8ed6f82 commit 8a813f1

File tree

6 files changed

+22
-16
lines changed

6 files changed

+22
-16
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#----------------------- PROJECT CONFIGURATION --------------------------------
2-
cmake_minimum_required(VERSION 3.10)
2+
cmake_minimum_required(VERSION 3.16)
33
project(cpp-dotenv VERSION 1.0.0)
44

5-
set(CMAKE_CXX_STANDARD 11)
5+
set(CMAKE_CXX_STANDARD 17)
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
77

88
if ("${CMAKE_BUILD_TYPE}" STREQUAL "")

common/libs/antlr4-cpp-runtime/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
cmake_minimum_required(VERSION 3.0.2)
1+
cmake_minimum_required(VERSION 3.10)
22
project(antlr4-cpp-runtime VERSION 4.8)
33

4-
set(CMAKE_CXX_STANDARD 11)
4+
set(CMAKE_CXX_STANDARD 17)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66

77
if ("${CMAKE_BUILD_TYPE}" STREQUAL "")

include/dotenv.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace dotenv
1515

1616
public:
1717

18-
dotenv& load_dotenv(const std::string& dotenv_path = env_filename,
18+
dotenv& load_dotenv(const std::string& dotenv_path = ".env",
1919
const bool overwrite = false,
2020
const bool interpolate = true);
2121

@@ -28,7 +28,7 @@ namespace dotenv
2828
void operator=(const dotenv&) = delete;
2929

3030
static dotenv& instance();
31-
31+
3232
private:
3333

3434
dotenv() = default;
@@ -42,4 +42,8 @@ namespace dotenv
4242

4343

4444
extern dotenv& env;
45+
46+
// Inline variables defined after class is complete (C++17)
47+
inline const std::string dotenv::env_filename = ".env";
48+
inline dotenv dotenv::_instance;
4549
}

src/common/logger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ bool logger::position::operator<(const position& p) const
2121
}
2222

2323

24-
bool logger::position::less::operator()(const position& p1, const position& p2)
24+
bool logger::position::less::operator()(const position& p1, const position& p2) const
2525
{
2626
return p1 < p2;
2727
}

src/common/logger.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace dotenv
3131

3232
struct less
3333
{
34-
bool operator()(const position& p1, const position& p2);
34+
bool operator()(const position& p1, const position& p2) const;
3535
};
3636

3737
public:

src/dotenv.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88

99

1010
using namespace std;
11-
using namespace dotenv;
1211

1312

14-
dotenv::dotenv& dotenv::dotenv::load_dotenv(const string& dotenv_path, const bool overwrite, const bool interpolate)
13+
// Typedef to work around the fact that class name matches namespace name
14+
// This is a known C++ limitation when defining out-of-line members
15+
typedef class ::dotenv::dotenv Dotenv_Type;
16+
17+
18+
Dotenv_Type& ::dotenv::dotenv::load_dotenv(const string& dotenv_path, const bool overwrite, const bool interpolate)
1519
{
1620
ifstream env_file;
1721
env_file.open(dotenv_path);
@@ -27,19 +31,17 @@ dotenv::dotenv& dotenv::dotenv::load_dotenv(const string& dotenv_path, const boo
2731
}
2832

2933

30-
const dotenv::dotenv::value_type dotenv::dotenv::operator[](const key_type& k) const
34+
auto ::dotenv::dotenv::operator[](const key_type& k) const -> const string
3135
{
3236
return getenv(k).second;
3337
}
3438

3539

36-
dotenv::dotenv& dotenv::dotenv::instance()
40+
Dotenv_Type& ::dotenv::dotenv::instance()
3741
{
3842
return _instance;
3943
}
4044

4145

42-
const string dotenv::dotenv::env_filename = ".env";
43-
dotenv::dotenv dotenv::dotenv::_instance;
44-
45-
dotenv::dotenv& dotenv::env = dotenv::instance();
46+
// Static members are now inline in the header (C++17)
47+
::dotenv::dotenv& ::dotenv::env = ::dotenv::dotenv::instance();

0 commit comments

Comments
 (0)