gpt4 book ai didi

c++ - 是否在其中带有 `T&&`并推导出 `T`的参数类型始终是通用引用?

转载 作者:行者123 更新时间:2023-12-03 07:09:13 25 4
gpt4 key购买 nike

引用

template<typename T>
void fun(ParamType param); // ParamType is some form of T, e.g. T, T&, T const, ...

fun(expr); // expr is an expression
我知道 &&中的 ParamType“某处”不足以使 ParamType成为通用引用,例如 ParamType = std::vector<T>&&是右值引用。 &&必须紧靠模板参数 T( T&&)才能成为通用引用。
但我也知道 T&&中的“某处”的 ParamType仍然不够用,例如 ParamType = std::remove_reference_t<T&&>并非通用引用。
因此,我几乎在问 ParamType的“表单”对转发引用有什么要求?
但是, by doing a bit of research,我觉得我需要问一个更“二进制”的问题:
为简单起见,假设 T是唯一的模板参数,因此 ParamType只是 T的函数(从数学上来说),是否正确地说这两个都是正确的
  • TParamType的形式出现在T&&
  • fun推导TPramType(即无需通过T传递fun<T>)

  • 那么 ParamType是通用引用吗?
    换句话说,如果 ParamType包含推导了 T&&T,它是否始终是通用引用?

    最佳答案

    what are the requirements on the "form" of ParamType for it to be a forwarding reference?.


    根据标准(最新草案):

    [temp.deduct.call] A forwarding reference is an rvalue reference to a cv-unqualified template parameter that does not represent a template parameter of a class template



    Is a parameter type with T&& in it and T deduced always a universal reference?


    类型“中”是什么都无关紧要,但是 是什么类型。如果它是对函数模板参数的cv不合格右值引用,则它是转发引用。
    例如, Foo<T&&>中包含“ T&&”,但它不是转发引用。 ParamType也不是转发引用,因为它不是函数的模板参数。

    关于c++ - 是否在其中带有 `T&&`并推导出 `T`的参数类型始终是通用引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64956682/

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