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
[dcl.dcl] Consider using less awkward example to illustrate static_assert #1514
Comments
I don't think we should use a library facility in the core language section unless necessary to show the issue.
Please make a concrete suggestion. |
Fair enough, here are some ideas:
|
What I find most offensive about the status quo is that it suggests that it's somehow OK to depend on the signedness of |
This is not a good check, since
That requires a #include. |
@tkoeppe: It's as "OK" to depend on the signedness of "char" as it is to depend on sizeof(void*) == 8 or CHAR_BIT == 8. Sometimes, you want to check for it because your algorithm breaks down otherwise, and getting a clean error message is much preferable over a wrong answer at run-time. |
@jensmaurer I see you responded to 2 of my 4 suggestions. How do you feel about the other 2? |
@jensmaurer: Hm, I think all of those would be in equally poor taste. Something more innocuous would be |
@Eelis: I'm not fully convinced by the suggestions; and I remember CWG having trouble coming up with a plausible example, too. |
Coming up with something that's obviously better is certainly a harder challenge than I expected! :) I also tried finding examples with github code search, but couldn't find anything that meets all the criteria. I guess if this has already been tried without success before, and we're ready to give up, then this can be closed. |
@jensmaurer I think platform-dependant assumptions are exactly what you would use static_assert for, outside of type/value-dependent contexts in templates. |
@AlisdairM: I agree, so what's wrong with the status quo: |
@jensmaurer: I've actually seen this exact thing in real code twice recently, and it was never appropriate :-( Checks for sizes and alignments are fairly plausible, say, if you want to build a buffer to hold arbitrary elements that get placement-constructed in it later, where the purpose of the static assertion is more to prevent misuse than to constrain the platform. |
@tkoeppe: Fine, but |
|
@jwakely: That one has the same issue as the status quo, namely that it would be more idiomatically expressed using a library facility. |
sizeof(int) == sizeof(void*)? |
Yeah, let's try that. |
Awesome, thanks guys! |
[dcl.dcl]/6 uses the following example to demonstrate the use of static_assert:
Perhaps this not the best example to use, since a test for signedness of char would probably be more idiomatically expressed using std::is_signed_v. Of course, since this is a core chapter, we may not want to use std::is_signed_v, but I suspect one could come up with an example static_assert that is illustrative, idiomatic, and does not use the stdlib.
The text was updated successfully, but these errors were encountered: