如何使用另一个模板作为参数初始化模板类?

时间:2019-06-10 11:56:00

标签: c++ templates

我试图更好地理解C ++模板,并且无法使以下代码按我想要的方式工作。

#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;
}

您可能会看到,Bowl具有接受硬编码的成分。我也希望它也可以作为模板类,这样我也可以添加Milk成分。

我以前有过这个

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};

但这说明:

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

我在这里做错什么,或者我不正确理解什么?

2 个答案:

答案 0 :(得分:1)

您尝试过

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

我的意思是...如果您定义

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

您认为T1Ingredient<Milliliters<double>>,而T2Ingredient<Grams<int>>

因此,构造函数等待Ingredient<T1>Ingredient<T2>,等待Ingredient<Ingredient<Milliliters<double>>>Ingredient<Ingredient<Grams<int>>>

Ingredient个。

答案 1 :(得分:1)

template可能很棘手,所以让我们考虑一下您的T1T2是什么。

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

由于您的Bowl构造函数使用Ingredient<T1>, Ingredient<T2>,因此扩展为:

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