-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1342 from davedissian/bugfix/sfinae-iterator-traits
Add a SFINAE friendly iterator_traits and use that instead.
- Loading branch information
Showing
5 changed files
with
125 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#pragma once | ||
|
||
#include <iterator> // random_access_iterator_tag | ||
|
||
#include <nlohmann/detail/meta/void_t.hpp> | ||
#include <nlohmann/detail/meta/cpp_future.hpp> | ||
|
||
namespace nlohmann | ||
{ | ||
namespace detail | ||
{ | ||
template <typename It, typename = void> | ||
struct iterator_types {}; | ||
|
||
template <typename It> | ||
struct iterator_types< | ||
It, | ||
void_t<typename It::difference_type, typename It::value_type, typename It::pointer, | ||
typename It::reference, typename It::iterator_category>> { | ||
using difference_type = typename It::difference_type; | ||
using value_type = typename It::value_type; | ||
using pointer = typename It::pointer; | ||
using reference = typename It::reference; | ||
using iterator_category = typename It::iterator_category; | ||
}; | ||
|
||
// This is required as some compilers implement std::iterator_traits in a way that | ||
// doesn't work with SFINAE. See https://github.com/nlohmann/json/issues/1341. | ||
template <typename T, typename = void> | ||
struct iterator_traits | ||
{ | ||
}; | ||
|
||
template <typename T> | ||
struct iterator_traits<T, enable_if_t<!std::is_pointer<T>::value>> | ||
: iterator_types<T> | ||
{ | ||
}; | ||
|
||
template <typename T> | ||
struct iterator_traits<T*, enable_if_t<std::is_object<T>::value>> { | ||
using iterator_category = std::random_access_iterator_tag; | ||
using value_type = T; | ||
using difference_type = ptrdiff_t; | ||
using pointer = T*; | ||
using reference = T&; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters