There are 2 problems:
First you used the wrong syntax, which was already explained in the answer of 康桓瑋.
The next problem is, that your concepts will always fail, because your set
and reset
functions are not const
. Because the non type template parameter must always be a compile time constant, you can't access member functions on it, which are non const.
The working example is given here:
template <auto gp>
concept DigitalOutput = requires()
{
gp.set();
gp.reset();
};
template <auto output> requires DigitalOutput<output>
struct driver
{
};
struct A
{
void set() const {} // must be const!
void reset() const {} // must be const!
};
struct B
{
};
int main()
{
A a;
driver<a> da;
B b;
driver<b> db; // fails as expected
}
Working example
For the problem of the const
of the member functions, we can do the following trick to overcome that problem:
#include <utility>
#include <type_traits>
template <auto gp>
concept DigitalOutput = requires()
{
// create a new value which is not const from the given non type template parm:
std::remove_const_t<decltype(gp)>().set();
std::remove_const_t<decltype(gp)>().reset();
};
template <auto output> requires DigitalOutput<output>
struct driver
{
};
struct A
{
void set() {}
void reset() {}
};
struct B
{
};
int main()
{
A a;
driver<a> da;
B b;
driver<b> db; // fails as expected
}
Working example
DigitalOutput auto output
should compile and do what you are after.