小编Bec*_*ted的帖子

使用const引用的变量模板特化

如何专门为参数提供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;
}
Run Code Online (Sandbox Code Playgroud)

当使用声明的const Test&参数调用函数foo时,编译器无法看到已删除函数的专用函数和回退:

 error: use of deleted function ‘T foo(Args ...) [with T = int; Args = {int, const char*, Test}]’
     auto i = …
Run Code Online (Sandbox Code Playgroud)

c++ template-specialization const-reference variadic-templates

8
推荐指数
2
解决办法
731
查看次数