基于值推导类型的非类型模板参数

时间:2014-02-14 10:57:53

标签: c++ templates

编写函数模板时,是否可以根据为该参数提供的值推导出非类型模板参数的类型?

Visual Studio接受此:

template<typename T, T Value> void foo(T x) {
    // some algorithm using x and Value goes here
}

void bar() {
    foo<int, -1>(42.0); // calls foo<T = int, Value = -1>(int)
}

但我想要做的就是这样(伪造的语法):

template<typename T Value> void foo(T x) {
    // some algorithm using x and Value goes here
}

void bar() {
    foo<-1>(42.0); // deduces the type of T from -1,
                   // calls foo<T = int, Value = -1>(int)
}

这是否可行(即,不必明确指定T的类型)?

0 个答案:

没有答案
相关问题