模板参数 - 指向模板化的类型的指针

template paramater - pointer to templated type

本文关键字:指针 类型 参数      更新时间:2023-10-16

考虑这个例子:

template< typename T, T &V>
void doSomething() {
    V = 1;
}
int i;
double d1, d2;
int main() {
    doSomething< int, i>();
    doSomething< double, d1>();
    doSomething< double, d2>();
    return 0;
}

是否可以消除调用中的类型名称?像这样:

    doSomething< i>();
    doSomething< d1>();
    doSomething< d2>();

请注意,函数签名不应更改。您仍然必须能够像这样使用它:

typedef void (*THandler)();
THandler handlers[] = {
    &doSomething< int, i>,
    &doSomething< double, d1>,
    &doSomething< double, d2>
};

是的。

template<typename T>
void doSomething(T& V) {
    V = 1;
}

但是你这样使用它:

doSomething(i);
doSomething(d1);
doSomething(d2);
相关文章: