DEV Community

Discussion on: Forbid a particular specialization of a template

Collapse
 
sandordargo profile image
Sandor Dargo • Edited

Just a bit of nitpicking. =delete is available also since C++11 according to CppReference

Deleted functions

Regarding 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:

#include <concepts>
#include <iostream>

template <typename T>
concept Number = (std::integral<T> || std::floating_point<T>) && (not std::same_as<T, bool>);

template <typename T> 
requires Number<T>
T f(T t) {
    return 2 * t + 3;
}

template <typename T> 
auto f2(T t) requires Number<T> {
    return 2 * t + 3;
}

template <Number T>
auto f3(T t) {
    return 2 * t + 3;
}

auto f4(Number auto t) {
    return 2 * t + 3;
}

int main () {
    std::cout << f(2) << std::endl;
    std::cout << f2(2.2) << std::endl;
    std::cout << f3(3) << std::endl;
    std::cout << f4(-2.4) << std::endl;
    // std::cout << f2(true) << std::endl; // auto f2(T) requires  Number<T> [with T = bool]' with unsatisfied constraints
}
Enter fullscreen mode Exit fullscreen mode

I'll write a more detailed post about the 4 ways of concepts.

_Please note that the concept Number is incomplete, it accepts char for example _

Collapse
 
pgradot profile image
Pierre Gradot • Edited

This is not being "nitpicking": this is being "accurate". Thanks! I fixed that.

In fact, I missed this warning:

warning: deleted function definitions are a C++11 extension [-Wc++11-extensions]

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 ; )

Collapse
 
sandordargo profile image
Sandor Dargo

Gosh, I started that article, and it's already longer than the whole series on constness. I'll have to break it down :)

Thread Thread
 
pgradot profile image
Pierre Gradot

Not surprising XD

Collapse
 
sandordargo profile image
Sandor Dargo

Thanks a lot for the PS :)

I have to start writing that article soon!