是否有可能重载一个函数以接受具有非类型模板参数的所有实例?

is it possible to overload a function to accept all instances of with a non-type template parameter

本文关键字:类型 参数 实例 重载 有可能 函数 一个 是否      更新时间:2023-10-16
template<typename T,int I=5> struct A{
    T _store[I];
};
template<typename T,int I>
void doSomething(A<T,I>& a){
  std::cout << "basic template for all other types" << std::endl;
}
template<>
void doSomething(A<int>& a){
 std::cout << "specialized integer template" << std::endl;
}
int main(int argc, char** argv){

    A<char> a;
    A<int> i;
    A<int,10> i10;
    doSomething(a);
    doSomething(i);
    doSomething(i10); //this does not call any specialized version yet
    return 0;
}

是否有一种方法可以声明doSomething专门化以接受所有A<int,...>实例,而不管第二个参数是什么,即使每个不同的A<int,...>在严格意义上是不同的类型,

如果我不需要在理论上声明和跟踪需要的每个不同的专门化,那么

实际上会使这个使用起来可行。

template<int I>
void doSomething(A<int, I> & value)
{...}