简体   繁体   English

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

[英]template paramater - pointer to templated type

Considering this example: 考虑以下示例:

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

Is it possible to eliminate type names in the invocations? 是否可以在调用中消除类型名称? Something like this: 像这样:

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

Note that the function signature shouldn't change. 请注意,函数签名不应更改。 You still have to be able to use it as this: 您仍然必须能够这样使用它:

typedef void (*THandler)();

THandler handlers[] = {
    &doSomething< int, i>,
    &doSomething< double, d1>,
    &doSomething< double, d2>
};

Yes. 是。

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

but you use it this way: 但是您可以通过以下方式使用它:

doSomething(i);
doSomething(d1);
doSomething(d2);

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

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