gpt4 book ai didi

c++ - std::add_lvalue_reference 和 std::add_rvalue_reference 的目的是什么?

转载 作者:行者123 更新时间:2023-12-02 07:31:09 26 4
gpt4 key购买 nike

std::add_lvalue_referencestd::add_rvalue_reference 的用途是什么?

看起来使用 T &/T && 的效果是一样的,因为这成功编译了:

#include <utility>

int main() {
{ using T = int; static_assert(std::is_same_v<std::add_lvalue_reference_t<T>, T &>); };
{ using T = int &; static_assert(std::is_same_v<std::add_lvalue_reference_t<T>, T &>); };
{ using T = int &&; static_assert(std::is_same_v<std::add_lvalue_reference_t<T>, T &>); };

{ using T = int; static_assert(std::is_same_v<std::add_rvalue_reference_t<T>, T &&>); };
{ using T = int &; static_assert(std::is_same_v<std::add_rvalue_reference_t<T>, T &&>); };
{ using T = int &&; static_assert(std::is_same_v<std::add_rvalue_reference_t<T>, T &&>); };
}

最佳答案

void&格式不正确。 std::add_lvalue_reference<void>void .

一般来说,add_lvalue_reference如果不可能,则不会添加对类型的引用。每[meta.trans.ref] :

template <class T>
struct add_­lvalue_­reference;

If T names a referenceable type then the member typedef type names T&; otherwise, type names T. [ Note: This rule reflects the semantics of reference collapsing ([dcl.ref]). — end note ]

什么是可引用类型?每[defns.referenceable] ,可引用的类型是

an object type, a function type that does not have cv-qualifiers or a ref-qualifier, or a reference type [ Note: The term describes a type to which a reference can be created, including reference types. — end note ]

关于c++ - std::add_lvalue_reference 和 std::add_rvalue_reference 的目的是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57819653/

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