C++ - template a constexpr pre dvojicu trieda - funkcia

Ahojte kolegovia, prisiel som si pre radu ku skusenejsim :)

majme nasledujuci kod (vid nizsie), kde su 3 triedy (A,B,C) a k nim prisluchajuce funkcie (A_graph, B_graph a C_graph) - 3rd libka.
Posladal som si Builder a Helper class. Helper sluzi na inicializaciu dat, je narocny, tak sa oplati ho vyskladat iba raz (samozrejme, pre danu A/A_graph ...)

Riesim ako elegantnejsie vyriesit Helper constructor, a jeho privatneho clena unique_ptr<(A/B/C)> graph = make_unique<(A/B/C)>(A_graph/B_graph/C_graph("path"))

Dakujem pekne za hinty!
M

Kód: [Vybrat]
#include <string>
#include <memory>
#include <type_traits>

using namespace std;


/* 3rd lib defs */
class A {
    /* .. */
};

A A_graph(const string & in) {
    A ret;
    /* ... */
    return ret;
};

class B {
    /* .. */
};

B B_graph(const string & in) {
    B ret;
    /* ... */
    return ret;
};

class C {
    /* .. */
};

C C_graph(const string & in) {
    C ret;
    /* ... */
    return ret;
};

/* Own classes */
class IResult {};
class XResult : IResult {};
class YResult : IResult {};
class ZResult : IResult {};


template <class G>
class Helper {
    private:
        unique_ptr<G> graph;
        /*
        unique_ptr<GraphCalculator> calc;
        unique_ptr<Contraction> conc;
        unique_ptr<Router> rtr;
        */
    public:
        Helper(const string & in){


            if(0){
            }else if constexpr (std::is_same<G, A>::value){
                graph = make_unique<A>(A_graph(in));
            }else if constexpr (std::is_same<G, B>::value){
                graph = make_unique<B>(B_graph(in));
            }else if constexpr (std::is_same<G, C>::value){
                graph = make_unique<C>(C_graph(in));
            }

            /* Some data - time consuming init and calculations
           
            calc = make_unique<Contraction>(graph);
            ...
            */
        };
};

template <class R, class G>
class Builder {
    private:
        unique_ptr<Helper<G>> builder;
    public:
        unique_ptr<R> build(void){
            if(!builder)
                builder = make_unique<Helper<G>>("path");
           
            R ret;

            /* some stuff
            ...
           
            auto calculate = MySuperCalculateFunction(builder->conc->results());
            ret.append(route);
           
             ... */
           

            return make_unique<R>(ret);
        };
};

using XABuilder = Builder<XResult, A>;

int main(){

    XABuilder builder;
    auto A = builder.build();

    return 0;
};

AD: poprosim upravit nazov temy, netusim ako som to mal pomenovat


Re:C++ - template a constexpr pre dvojicu trieda - funkcia
« Odpověď #1 kdy: 12. 01. 2021, 16:23:35 »
Co třeba takto:
Kód: [Vybrat]
#include <memory>
#include <string>
#include <variant>

struct A{};
struct B{};
struct C{};

template<typename T>
struct Tag{};

A makeGraph(const std::string& in, Tag<A>) {
    return A{};
}
B makeGraph(const std::string& in, Tag<B>) {
    return B{};
}
C makeGraph(const std::string& in, Tag<C>) {
    return C{};
}

template<typename T>
std::variant<A, B, C> makeGraph(const std::string& in) {
    return makeGraph(in, Tag<T>{});
}

int main() {
    auto graph = makeGraph<A>({});
    return 0;
};
Místo různých názvů vytvořujících funkcí, a následného použití constexpr if, stačí odlišit vytvořující funkce pomocí parametru.
Variant se dá použít pro uložení tříd bez společného předka.