Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unable to call my operator<T t>() if it doesn't receives arguments

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

PS: I’m using G++ with -std=c++20

>Solution :

Just use the explicit notation for the call:

myState.operator()<Derived>();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading