-
Notifications
You must be signed in to change notification settings - Fork 108
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
Fix implementation of gsl_REQUIRES_T_() #238
Conversation
…ct 40) Fixes implementation of gsl_REQUIRES_T_(), see martinmoene/nonstd-lite-project#40 and https://en.cppreference.com/w/cpp/types/enable_if#Notes
Note: Visual Studio 2013 (VC12) fails with warning C4346 dependent name is not a type Update: changed to fall back to previous behavior for VS2013. |
# if gsl_BETWEEN( gsl_COMPILER_MSVC_VERSION, 1, 140 ) | ||
// VS 2013 and earlier seem to have trouble with SFINAE for default non-type arguments | ||
# define gsl_REQUIRES_T_(VA) , typename = typename std::enable_if< ( VA ), ::gsl::detail::enabler >::type | ||
# else | ||
# define gsl_REQUIRES_T_(VA) , typename std::enable_if< ( VA ), int >::type = 0 | ||
# endif |
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.
What is the reason for this change? I understand that we want to use typename std::enable_if< X, int >::type = 0
rather than typename = typename std::enable_if< X >::type
in function templates, but we already have gsl_REQUIRES_A_()
for that purpose.
If the latter form is not required anywhere, perhaps we should remove gsl_REQUIRES_T_()
altogether?
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.
Ah great, you awoke me :) I have generalized the note about function templates to include class templates :)
Thus there is no reason for this change left.
@@ -1085,13 +1090,12 @@ struct is_compatible_container : std17::bool_constant | |||
|
|||
template< | |||
class C, class E | |||
, typename = typename std::enable_if< | |||
gsl_REQUIRES_T_(( |
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.
Note that SFINAE via default template type arguments is fine in class definitions. (The two subsequent arguments class = decltype( ... )
also use default template type arguments for SFINAE.)
Also, our SFINAE Guidelines say that the gsl_REQUIRES_*_()
macros should be avoided "if language support can be assumed in the given context", which is the case here.
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.
Commenting at the toplevel.
Cf. #197 (comment), #197 (comment) for previous discussion of SFINAE and |
Thanks for you remarks and pointing to the discussion & comments (which i had (mostly) seen, but forgotten about). Given all what I read, it's perhaps best to 'just' close this PR :) This PR results from a change I made in the other *-lite libraries on the premise, erroneously generalized to templated classes, that for sfinae in a template parameter list
is to be preferred over
based on this note over at CppReference. On naming From what I recall:
In hindsight, more appropriate letters might perhaps have been choosen (and perhaps still can:). It looks like gsl-lite contains several uses of Code over requires() clause: I'm always a little surprised over a preference for literal code over a searchable, contract-like
|
That makes sense. But as per the discussion I linked to, function argument SFINAE should normally not be used (this is needed only for constructors in pre-C++11 and for buggy compilers). I figured that
According to your definition, yes. I admit I don't know off hand whether non-type argument SFINAE is actually wrong for class template definitions; ideally we could use this form everywhere. I remember having had trouble even with recent MSVC versions when I tried this though. I'll investigate.
I'd argue
The last point is also why the current SFINAE guidelines request that Eli Bendersky's article on SFINAE, which was a valuable resource for me, comes to a similar conclusion:
Edit: I acknowledge your point that the macro attempts to look like a "contract-like |
Thanks for your extensive reply. I can lean to your conclusions as well. On the aspect of clarity by refraining from using macro I think we've reached closure :)
However in the specifications of std::expected, std::optional, std::variant, std::span restrictions on methods abound. |
I'm grateful you still care about that child you have let go :)
Yes, and I guess I've gotten so accustomed to syntax overhead that I completely overlooked this aspect of the macro. On reviewing gsl-lite, I agree that the constraints written with the macro are much easier to grasp than those where
True, SFINAE is often needed when defining vocabulary types... So I guess as long as we make it clear that the macro is for internal use (which was my intent in adding the trailing Reassessing the uses of
So perhaps we can simplify this a little:
Then we'd have only a single macro flavor, its name would no longer allude to concepts, and searching for "enable_if" would also find the macro. And we would be consistent again by using What do you think? |
@mbeutel , will react later |
You do make an interesting suggestion to see if all constraints might be taken under the wings of a single macro like I think that would be a nice result. That said, if you prefer using plain |
Fixes implementation of
gsl_REQUIRES_T_()
, see nonstd-lite-project issue 40 and enable_if on CppReference.