vendredi 25 octobre 2019

Is it possible to call a class method and a global method based on the parameters of a class template?

This is my problem:

#include <string>



struct Print
{
    template <typename T>
    void Printer( const T& data )
    {
        PrinterInstance( data );
    }


    void PrinterInstance( const int& data )
    {
        printf( "INTEGER\n" );
    }
};



void PrinterInstance( const std::string& data )
{
    printf( "STRING\n" );
}



int main()
{
    Print print;
    print.Printer( "3" );
    return 0;
}

Inside the class Print, I have a template Printer that basically calls PrinterInstance based on the parameters of the template. Also, I should be able to extend this funcionality by adding more PrinterInstance outside of the class.

But this does not compile. If I implement PrinterInstance only inside the class, it's ok. If I implement PrinterInstance only outside the class, it's also ok. But as soon I have one PrinterInstance inside the class and one outside the class, the template will only try to use the class one.

How can I make this work?

Aucun commentaire:

Enregistrer un commentaire