如何专门化具有参数的 const 引用的可变参数模板函数?
例子:
template<typename T, typename... Args>
T foo(Args... args) = delete;
template<> int foo(int a, const char* str, const Test& t) { .... } // Fails to compile
//template<> int foo(int a, const char* str, Test ) { .... } // Ok
int main() {
auto i = foo<int>(10, "test string!", t);
return 0;
}
当使用声明的参数调用函数 foo 时const Test&
,编译器看不到专门的函数并回退到已删除的函数:
error: use of deleted function ‘T foo(Args ...) [with T = int; Args = {int, const char*, Test}]’
auto i = foo<int>(10, "test string!", t);
如果我从参数中删除 const 引用,上面的代码编译得很好。我究竟做错了什么?
代码可以在这里找到