考虑以下示例:

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

关于c++ - 模板参数-指向模板类型的指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19185167/

10-12 19:14