简体   繁体   English

对模板参数的困惑

[英]Confusion about template parameters

Lately I practiced use of template in c++, and one confusion about the template parameters during the process.最近在c++练习template的使用,过程中对模板参数有一个困惑。

For instance, in the following code, the input A of template<typename A> served as a 'type' to construct function and variables.例如,在下面的代码中, template<typename A>的输入A作为“类型”来构造 function 和变量。

template <typename A>
A add_simple(A a, A b)
{
    return a + b;
}

However, in the following example, a unit conversion operation, I implemented, it seems the input M1 and M2 serve as objects.但是,在下面的示例中,我实现了一个单位转换操作,输入M1M2似乎充当了对象。 In main() , I've shown how the struct be expected called.main()中,我展示了如何调用该结构。

// create a template struct Measure with value and unit. e.g. Measure<int, Unit>::value
template<int v, Unit u> struct Measure{
    static const int value=v;
    static const Unit unit=u;
};

// Conversion struct. expected input two 'Measure' objects. 
template <typename M1, typename M2> 
struct Measure_add {
public:
    // other implementation

    static constexpr int value=M1::value+M2::value;
};

int main(int, char**) {

    std::cout << Measure_add< Measure<10,Unit::m>,
        Measure<20,Unit::m> >::value << std::endl;

}

So my confusion about template is:所以我对模板的困惑是:

  1. Is <template> designed to be such flexible that it dynamically distinguish the input so both a 'type' input and 'object' input works. <template>是否设计得如此灵活,以至于它可以动态区分输入,因此“类型”输入和“对象”输入都可以工作。 Or the difference here I met is because the expected inputs are constructed template input Measure ?或者我在这里遇到的不同是因为预期输入是构造模板输入Measure As a result, M1 and M2 are still 'type', Measure .因此,M1 和 M2 仍然是“类型”,即Measure And in this case, why M1::value is available if M1 is a type instead of an object?在这种情况下,如果 M1 是一种类型而不是 object,为什么M1::value可用?

  2. Why is it available to get access to M1::value or M2::value in template struct Measure_add though the inputs are unknown to such struct.为什么可以访问模板结构Measure_add中的M1::valueM2::value ,尽管此类结构的输入未知。 In other words, if I input other parameters which don't contain attribute value , would it cause problem?换句话说,如果我输入其他不包含属性value的参数,是否会引起问题?

In your code both M1 and M2 are type template parameters.在您的代码中, M1M2都是类型模板参数。 The reason why you can say M::value is that value is a static data member in a type that is substituted for M .您可以说M::value的原因是value是一个 static 类型的数据成员,该类型替代了M That's the point of a static data member: you don't need an object to access its value.这就是static数据成员的要点:您不需要 object 来访问它的值。 This is not related to templates:这与模板无关:

struct M {
    static constexpr int value = 0;
};

int foo() {
    return M::value;   // OK
}

M1 and M2 are not constrained to being specializations of Measure template: they can be any types that have a static value data member. M1M2不限于Measure模板的特化:它们可以是具有 static value数据成员的任何类型。 For other types that don't satisfy this condition you'll simply get a compilation error.对于不满足此条件的其他类型,您只会收到编译错误。

std::cout <<
    Measure_add<
        std::integral_constant<int, 10>,
        std::integral_constant<int, 20>
    >::value;  // compiles, prints 30

and

std::cout << 
    Measure_add<
        Measure<10, Unit::m>,
        std::integral_constant<int, 20>
    >::value;  // compiles, prints 30, but makes no sense

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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