gpt4 book ai didi

d - D 中的完美转发?

转载 作者:行者123 更新时间:2023-12-04 15:36:27 26 4
gpt4 key购买 nike

tl;dr:你好吗perfect forwarding在 D?

该链接有一个很好的解释,但例如,假设我有这个方法:

void foo(T)(in int a, out int b, ref int c, scope int delegate(ref const(T)) d)
const nothrow
{
}

我如何创建另一种方法, bar() ,可以代替 foo() 调用,随后调用 foo() “完美”(即在调用站点不引入编译/范围/等问题)?

天真的方法
auto bar(T...)(T args)
{
writeln("foo() intercepted!");
return foo(args);
}

当然不起作用,因为它不处理 ref , in , out , inout , const -方法的完整性, pure -ity, nothrow等...,它还限制了如何将这些值与 r 值一起使用。

而且我不知道如何处理这些可能的情况……有什么想法吗?

最佳答案

你的天真方法可以改进,尽管它仍然不完美:

auto ref bar(T...)(auto ref T args)
{
writeln("foo() intercepted!");
return foo(args);
}

现在唯一的问题是 scope论据。

关于d - D 中的完美转发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7948865/

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