-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a SFINAE friendly iterator_traits and use that instead. #1342
Merged
nlohmann
merged 3 commits into
nlohmann:develop
from
dgavedissian:bugfix/sfinae-iterator-traits
Dec 19, 2018
+125
−20
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>> { | ||
typedef std::random_access_iterator_tag iterator_category; | ||
typedef T value_type; | ||
typedef ptrdiff_t difference_type; | ||
typedef T* pointer; | ||
typedef T& reference; | ||
}; | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer
using
, but that's a minor thingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. @davedissian could you please make this last change. Then it should be good to go. Thanks!