gpt4 book ai didi

c++ - 为什么类型别名决定输出是左值还是右值?

转载 作者:太空狗 更新时间:2023-10-29 22:55:04 25 4
gpt4 key购买 nike

出于测试目的,我创建了一个包含两个静态函数的结构。 f 的第一个实例在传递 l-value reference 时被调用。当传递 r-value 时调用第二个实例:

template <typename _Tp>
struct T {
static constexpr void f(_Tp&) { std::cout << "f(T&) is called!\n"; }
static constexpr void f(_Tp&&) { std::cout << "f(T&&) is called!\n"; }
};

当我试验强类型时,我发现第一个实例,当我试图隐式创建强类型时调用了T::f(_Tp&) .为什么是这样? (见下文)

using T_int = T<int>;

T_int::f(
typename strong_types::create_strong_type<int, struct tag>(5)()
); // calls f::(T&) (?)

using KG = typename strong_types::create_strong_type<double, struct KG_tag>;
T_int::f(KG(4.2)()); // calls f(T&&)

请注意,operator() 返回通过构造函数给定的值。

请随时询问我是否需要详细说明。

编辑:strong_types 是一个命名空间。它存在于别名 create_strong_type 的其他事物中:

namespace strong_type {
template <typename T, typename tag>
using create_strong_type = Strong_Type<T, tag>;

...
}

...

template <typename T, typename tag>
struct Strong_Type {
constexpr explicit Strong_Type(const T& value) : _value(value) {}
constexpr explicit Strong_Type(T&& value) : _value(std::move(value)) {}

constexpr T& operator()() noexcept { return _value; }

private:
T _value;
};

最佳答案

区别不是因为使用了别名(using),而是因为您作为第一个模板参数传递给create_strong_type 的类型。 .在一种情况下,它是 int ,在另一个中,一个 double .

尝试 T<double>::f(KG(4.2)());你会看到参数作为左值引用传递(因为 Strong_Type::operator() 的返回类型,即 T& )。

关于c++ - 为什么类型别名决定输出是左值还是右值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53379373/

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