I’m trying to create a State class that will be used like an enum.
template <class ApplicationType>
class State
{
public:
template<class T>
requires requires(){ std::is_base_of<ApplicationType, T>::value; }
unsigned int operator()() //broken operator
{
static unsigned int localCounter = counter++;
return localCounter;
}
template<class T>
requires requires(){ std::is_base_of<ApplicationType, T>::value; }
unsigned int operator()(T t) //working operator
{
static unsigned int localCounter = counter++;
return localCounter;
}
private:
unsigned int counter = 0;
};
My problem is that when I call the first operator State::operator()() the code doesn’t compile, but when I call the second one State::operator()(T t) everything works fine.
class Base{};
class Derived: Base{};
int main()
{
State<Base> myState;
Derived d;
myState<Derived>(); // Error
myState(d); // OK
return 0;
}
Error message:
expected primary-expression before ‘>’ token
expected primary-expression before ‘)’ token
PS: I’m using G++ with -std=c++20
>Solution :
Just use the explicit notation for the call:
myState.operator()<Derived>();