C++是否可以超载一个函数,以便其参数分别接受文字和参考?

发布于 2025-02-01 20:51:02 字数 452 浏览 3 评论 0原文

我在使用C ++过载方面有问题,并且想知道是否有人可以提供帮助。

我正在尝试超负荷功能,以便其参数分别接受参考和文字。

例如,我想超载func1func2 to func

int func1 (int literal); 
int func2 (int &reference);

我想在这种情况下使用func

func(3);   // call func1
int val = 3;
func(val); // I want func2 to be called, but ambiguous error

有什么方法可以超载这些功能?

谢谢!任何帮助将不胜感激! 对不起,英语不好。

I'm having a problem with using C++ overloading and was wondering if anybody could help.

I'm trying to overload functions so that its argument accept reference and literal respectively.

For example, I want to overload func1 and func2 to func:

int func1 (int literal); 
int func2 (int &reference);

and I want to use func in this situations:

func(3);   // call func1
int val = 3;
func(val); // I want func2 to be called, but ambiguous error

Is there any way to overload these functions?

thanks! Any help would be appreciated!
sorry for poor english.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま柒月 2025-02-08 20:51:02

文字和临时值只能作为const引用传递,而命名值则可以在可用的情况下使用非const参考。您可以将其与&&&一起创建2个过载。

有关原因和更多详细信息,请阅读 rvalues” prvalues

下面的代码显示了最常见的情况将使用哪种函数过载,第一个是您询问的函数:

#include <iostream>

void foo1(int &) { std::cout << "int &\n"; }
void foo1(const int &) { std::cout << "const int &\n"; }

void foo2(int &) { std::cout << "int &\n"; }
void foo2(const int &) { std::cout << "const int &\n"; }
void foo2(int &&) { std::cout << "int &&\n"; }
void foo2(const int &&) { std::cout << "const int &&\n"; }

int bla() { return 1; }

int main() {
    int x{}, y{};
    std::cout << "foo1:\n";
    foo1(1);
    foo1(x);
    foo1(std::move(x));
    foo1(bla());
    std::cout << "\nfoo2:\n";
    foo2(1);
    foo2(y);
    foo2(std::move(y));
    foo2(bla());
}

输出:输出:

foo1:
const int &
int &
const int &
const int &

foo2:
int &&
int &
int &&
int &&

Literals and temporary values can only be passed as const references while named values will prefer a non-const reference if available. You can use this with either & or && to create the 2 overloads.

For why and more details read up on rvalues, lvalues, xvalues, glvalues and prvalues.

The code below shows which function overload will be used for the most common cases, the first 2 being the ones you asked about:

#include <iostream>

void foo1(int &) { std::cout << "int &\n"; }
void foo1(const int &) { std::cout << "const int &\n"; }

void foo2(int &) { std::cout << "int &\n"; }
void foo2(const int &) { std::cout << "const int &\n"; }
void foo2(int &&) { std::cout << "int &&\n"; }
void foo2(const int &&) { std::cout << "const int &&\n"; }

int bla() { return 1; }

int main() {
    int x{}, y{};
    std::cout << "foo1:\n";
    foo1(1);
    foo1(x);
    foo1(std::move(x));
    foo1(bla());
    std::cout << "\nfoo2:\n";
    foo2(1);
    foo2(y);
    foo2(std::move(y));
    foo2(bla());
}

Output:

foo1:
const int &
int &
const int &
const int &

foo2:
int &&
int &
int &&
int &&
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文