简体   繁体   中英

How to initialize template class with another template as parameter?

I'm trying to get a better understanding of C++ templates, and am not able to get the below code to work the way I want.

#include <iostream>
template <typename T>
class Amount {
public:
    T m_amount;

    Amount(T amount) : m_amount(amount) {
        //std::cout << __PRETTY_FUNCTION__ << "\n";
    }

    friend std::ostream &operator<<(std::ostream &out, const Amount &amount) {
        out << amount.m_amount;
        return out;
    }
};

template <typename T>
class Grams : public Amount<T> {
public:
    Grams(T amount) : Amount<T>(amount) {}
};

template <typename T>
class Milliliters : public Amount<T> {
public:
    Milliliters(T amount) : Amount<T>(amount) {}
};


template <typename T>
class Ingredient {
public:
    Amount<T> m_amount;
    std::string m_name;
    Ingredient(Amount<T> amount, std::string name) : m_amount(amount), m_name(name)
    {
        //std::cout << __PRETTY_FUNCTION__ << "\n";
        std::cout << "Ingredient name: " << m_name << ", amount: " << m_amount << "\n";
    }
};


class Bowl {
public:
    Ingredient<Milliliters<int>> m_ingredient1;
    Ingredient<Grams<int>> m_ingredient2;
    Bowl(Ingredient<Milliliters<int>> ingredient1, Ingredient<Grams<int>> ingredient2) :
    m_ingredient1(ingredient1),
    m_ingredient2(ingredient2)
    {
        //std::cout << __PRETTY_FUNCTION__ << "\n";
        std::cout << "Bowl with ingr1: " << m_ingredient1.m_name << ": " << m_ingredient1.m_amount << "\n";
        std::cout << "          ingr2: " << m_ingredient2.m_name << ": " << m_ingredient2.m_amount << "\n";
    }
    void Mix() {
        std::cout << "Mixing all ingredients in the bowl\n";
    }

};

int main() {

    Milliliters<int> amount_water {10};
    Milliliters<double> amount_milk {5.5};
    Grams<double> amount_flour {5.6};
    Grams<int> amount_butter {250};

    std::string name_water { "water" };
    std::string name_milk { "milk" };
    std::string name_flour { "flour" };
    std::string name_butter { "butter" };

    Ingredient<Milliliters<int>> Water {amount_water, name_water};
    Ingredient<Grams<int>> Butter {amount_butter, name_butter};

    Bowl bowl1 {Water, Butter};

    bowl1.Mix();

    return 0;
}

As you might see, the Bowl has a hardcoded accepted ingredient. I want that to be a template class as well, so that I would be able to add a Milk ingredient as well.

I had this before:

template <typename T1, typename T2>
class Bowl {
public:
    Ingredient<T1> m_ingredient1;
    Ingredient<T2> m_ingredient2;
    Bowl(Ingredient<T1> ingredient1, Ingredient<T2> ingredient2) :

[...]

    Ingredient<Milliliters<double>> Milk {amount_milk, name_milk};
    Ingredient<Grams<int>> Butter {amount_butter, name_butter};

    Bowl<Ingredient<Milliliters<double>>, Ingredient<Grams<int>>> bowl1 {Milk, Butter};

But that says:

No matching constructor for initialization of 'Bowl<Ingredient<Milliliters<double> >, Ingredient<Grams<int> > >'

What am I doing wrong here, or what do I not understand correctly?

Have you tried with

Bowl<Milliliters<double>, Grams<int>> bowl1 {Milk, Butter};
// ..^^^^^^^^^^^^^^^^^^^..^^^^^^^^^^   no more "Ingredient"

?

I mean... if you define a

Bowl<Ingredient<Milliliters<double>>, Ingredient<Grams<int>>>

you have that T1 is Ingredient<Milliliters<double>> and T2 is Ingredient<Grams<int>> .

So the constructor, waiting for Ingredient<T1> and Ingredient<T2> , waits for Ingredient<Ingredient<Milliliters<double>>> , and Ingredient<Ingredient<Grams<int>>>

Too much Ingredient s.

template s can be tricky, so let's think about what your T1 and T2 are.

Bowl<Ingredient<Milliliters<double>>, Ingredient<Grams<int>>>
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^  ^~~~~~~~~~~~~~~~~~~~~^
                  T1                           T2

Since your Bowl constructor uses Ingredient<T1>, Ingredient<T2> , this expands to:

Bowl<Ingredient<Ingredient<Milliliters<double>>>, Ingredient<Ingredient<Grams<int>>>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM