samedi 23 novembre 2019

C++ templates specialization for signature template class

I am creating signature template class. I am facing a problem for "void" return type. Here is my solution to handle void return type, which is I create a template specialization class for it.

template<typename TSignature>
class CAction;

template<typename TRetType, typename... Args>
class CAction<TRetType(Args...)> {
public:

    TRetType func(Args... a)
    {
        cout << "non void" << endl;
        myPrint(a...);
        cout <<"\n\n";
        TRetType nRet(0);
        return nRet;
    }
};

template<typename... Args>
class CAction<void(Args...)> {
public:
    void func(Args... a)
    {
        cout << "void" << endl;
        myPrint(a...);
        cout << "\n\n";
    }
};

Below is how I initialize the class.

CAction< void(int a, int b, double c, std::string d)> on_action1;
on_action1.func(1, 2, 10.0, "a love b");

CAction< double(int a, int b, double c, std::string d)> on_action2;
on_action2.func(1, 2, 10.0, "a love b");

The code above work correctly. I am just curious, besides above method, is there any better solution ? For example : May I create a template specialization member function (func) to handle "void" return type ? Please show me code example if you know more details, thank you very much.

Aucun commentaire:

Enregistrer un commentaire