mercredi 1 mars 2017

Function specializations in a class Template c++03/c++11 performance

I have two template classes and normaly i would prefer the "foo" version of the "Search" class. But sadly my compiler can not understand the "foo" version because of the mssing c++11 support. (He will not understand this line: template <typename U = _Format>)

The "bar" version will create an instance of the "_Format" type to call the right delegate. So also a constructor is called! Would this be a performance issue compared to the "Foo" version? (I know this would be this is like premature optimization, but i'm interested)

#include <iostream>

struct format1 {
    format1(void) { }
};
struct format2 {
    format2(void) { }
};

namespace foo
{

template <typename _Format>
class Search
{
public:
    void createData()
    {
        doCreateData();
    }

private:
    template <typename U = _Format>
    void doCreateData();

    template <>
    void doCreateData<format1>()
    {
        using namespace std;
        cout << "Format1" << endl;
    }

    template <>
    void doCreateData<format2>()
    {
        using namespace std;
        cout << "Format2" << endl;
    }
};

}

namespace bar
{

template <typename _Format>
class Search
{
    public:
        void createData(void)
        {
            doCreateData(_Format());
        }

    private:
        void doCreateData(format1)
        {
            using namespace std;
            cout << "Format1" << endl;
        }

        void doCreateData(format2)
        {
            using namespace std;
            cout << "Format2" << endl;
        }

};

}

int main(int argc, char *argv[])
{
    (void)argc; (void)argv;

    bar::Search<format2> search;
    search.createData();
}

Aucun commentaire:

Enregistrer un commentaire