gpt4 book ai didi

c++ - C++:使用参数包显式调用函数重载

转载 作者:行者123 更新时间:2023-12-02 10:07:42 27 4
gpt4 key购买 nike

如何使用参数包调用函数的重载版本?这大概是我想做的:

void foo(int x=5) {
// Call foo<Args...>(x) here
}

template <typename... Args>
void foo(int x, Args&&... args) {
}

那可能吗?还是我需要不同的函数名?

最佳答案

您可以通过显式指定模板参数来调用模板版本。如果没有要指定的模板参数,则可以指定空列表。例如

template <typename... Args>
void foo(int x, Args&&... args) {
}

void foo(int x=5) {
foo<>(x);
}

关于c++ - C++:使用参数包显式调用函数重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59349574/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com