Let's imagine a simple template function that performs basic numerical computations:
template <typename T>
T f(T t) {
return 2 * t + 3...
For further actions, you may consider blocking this person and/or reporting abuse
Small typo at the beginning
I also want to point out that the
static_assertapproach has a major drawback compared to the other ones. It prevents you to accurately detect if your functionfis callable with abool. Indeed with thestatic_assertyou have a function that is defined and takes part in overload resolution and simply produces an error when actually compiled.I you were using a library that, for some reason, wants to call your function with a
boolif supported, else call with anint, thestatic_assertwould fail to compile because the library would think that the function is callable with abooland attempt to do so before encountering thestatic_assert. With the other 2 approach, the library would correctly detect that the function cannot be called with abooland will instead call it with anint.Granted, this particular situation is unlikely to happen but there is a lot of similar situation in the standard library and in other library where something like that could occur. This is why
static_assertis not used much in the standard library but instead SFINAE is used (hopefully replaced by concepts in the future).Thanks, I have corrected this mistake.
I completely understand the principle. But it is not clear to me how would you write such a code. Can you provide a basic example please?
Hmm, I wasn't able to implement such detection for a free function because function template don't have a type (only concrete instantiation do).
By wrapping the function inside a
structI can showcase what I was talking about (because now I can make a template that takes mystructas a parameter).godbolt.org/z/z5a1h6
All in all maybe this is not really a problem with free function but only with member function (static or not).
Just a bit of nitpicking.
=deleteis available also since C++11 according to CppReferenceRegarding concepts, it's worth to note (?) that in case you only want to accept numbers (I'm a bit vague) you have 4 different ways:
I'll write a more detailed post about the 4 ways of concepts.
_Please note that the concept
Numberis incomplete, it acceptscharfor example _This is not being "nitpicking": this is being "accurate". Thanks! I fixed that.
In fact, I missed this warning:
I have not really dived into concepts yet, so I am really interested by your future article!
PS: I have added a PS to my article above ; )
Gosh, I started that article, and it's already longer than the whole series on constness. I'll have to break it down :)
Not surprising XD
Thanks a lot for the PS :)
I have to start writing that article soon!