如何使用 std::forward 在构造函数中使 const 仅左值

主义
template < typename T1, typename T2 >
MyClass(T1 && v1, T2 && v2)
    : m_v1(std::forward< T1 >(v1))
    , m_v2(std::forward< T2 >(v2))
{}

让我们有一个名为 的类MyClass,它的构造函数如上所示。如果没有std::forward我们必须编写 4 个不同的构造函数:

MyClass(SomeType&& v1, SomeType&& v2);
MyClass(SomeType&& v1, const SomeType& v2);
MyClass(const SomeType& v1, SomeType&& v2);
MyClass(const SomeType& v1, const SomeType& v2);

这里我们使用const SomeType&,因为我们不想改变我们的左值。当然,我们有const_cast,但很容易找到这样的cast同时,我们的模板构造函数 withstd::forward生成参数类型如下的构造函数:SomeType&- 没有const,因此我们可以更改构造函数体中的左值。

问题:正确的解决方法是什么?我应该添加const(怎么做?)还是只是以适当的方式编写我的构造函数的主体 - 所以,它们不会改变左值。在添加的情况下,const我们将得到这样的结果:const T1&&如果我们将右值赋予构造函数,我们将无法移动它,因为它将是常量右值。我想,const只有当我们给出左值时,我们才必须添加

我试图在n = 555;这里阻止

#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <memory>
#include <ciso646>

class A
{
public:
    using number_t = std::int32_t;
    using string_t = std::string;

    template < typename T >
    using vector_t = std::vector < T >;

public:
    template < typename T1, typename T2, typename T3, 
        typename Dummy = std::enable_if_t < std::is_same < number_t, 
        typename std::decay < T1 > ::type > ::value > >
    A(T1 && n, T2 && s, T3 && v) :
        m_n { std::forward < T1 > (n) },
        m_s { std::forward < T2 > (s) },
        m_v { std::forward < T3 > (v) } 
    {
        n = 555;
    }

public:
    number_t              m_n;
    string_t              m_s;
    vector_t < number_t > m_v;
};

int main()
{
    A::number_t                 n { 666 };
    A::string_t                 s { "hello" };
    A::vector_t < A::number_t > v { 1, 2, 3 };

    std::cout << n << std::endl;

    A a1(n, s, v);
    A a2(1, "hello", std::vector<A::number_t>( { 4, 5, 6 } ));

    std::cout << n << std::endl;

    A a3(1, "hello", std::vector<int>( { 4, 5, 6 } ));

    return 0;
}
尼可波拉斯

我试图在n = 555;这里阻止

您通过转发引用获取参数。这意味着您的意图是他们推断出的行为最终取决于调用者提供的内容。

如果调用者提供了一个非const左值,那么你会得到一个非const左值。如果调用者提供了一个const左值,那么这就是你得到的。

由于您打算将这些参数转发到它们的最终对象中,因此您的目的是在传递右值时能够修改它们。因此你不能制造它们const

保持原样。防止的方法n = 555;不写

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么在概念中使用std :: forward?

std :: forward如何工作?

std :: forward和copy构造函数

在移动构造函数中使用std :: move

构造函数使用std :: forward

使用删除的函数'std :: thread :: thread(const std :: thread&)'

为什么在右值引用构造函数的初始化列表中为数据成员使用std :: forward而不是std :: move?

没有可变参数调用std :: forward(const std :: string&)的匹配函数

在Haskell中使用值构造函数

在复制构造函数中使用* this作为const克隆自身

C ++:使用委托构造函数时选择`const char *`vs`std :: string`

如何使用std :: array构造函数参数C ++列表初始化const std :: array成员

如何在构造函数中使用std :: optional?

如果将forward_as_tuple结果存储在变量中,然后在std :: apply中使用它,则会丢失右值引用

错误:在初始化列表中使用std :: forward

在std :: vector构造函数中使用'{}'作为结束迭代器

为什么std :: move复制右值或const左值函数参数的内容?

在std :: forward中使用std :: decay

如何在函数的声明中使用const

在类构造函数中设置std :: vector,为元素构造函数使用不同的值

在构造函数中使用输出参数函数初始化const成员的调用的代码比lambda简单

如何在类构造函数中使用参数初始化std :: array的大小?

当使用std :: make_pair时,左值指定const对象

如何使用基于函数的值填充const std :: array <size_t,N>

在不使用std :: forward的情况下如何调用成员变量的move构造函数?

std::forward 转发函数

在构造函数初始化列表中使用 std::variant

使用 const string& 构造函数和模板化 std::set find 出现分段错误

std::forward 不允许接受左值