Call a static method of a class derived from template without specifying template

488 Views Asked by At

I will try to explain my problem with a simple example:

class UselessClass {};

template<class RUNNABLE>
class Task : public RUNNABLE
{
public:
    static void StartScheduler()
    {
        //Start system scheduler
    }
};

main.cpp

Task<UselessClass>::StartScheduler(); //Correct

//Task::StartScheduler();   //Wrong

What I want to do is write the following code: Task::StartScheduler();

1

There are 1 best solutions below

7
Rinat Veliakhmedov On

You can't. Template is not a class, you can't call it without providing template parameters so that the compiler would know the exact type to call methods from.

When you try using Task::StartScheduler();, the compiler has no way to know what is the type of the RUNNABLE template parameter.

You can do something like this:

using UselessTask = Task<UselessClass>;
UselessTask::StartScheduler();

Though it is not exactly what you wanted,