如何利用模板复制和移动构造函数和赋值运算符? [英] How to utilize template copy&move constructor and assignment operator?

查看:79
本文介绍了如何利用模板复制和移动构造函数和赋值运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下C ++代码,以尝试避免对非模板复制和移动构造函数和赋值运算符的偏好:

Consider the following C++ code with my failed attempt to avoid preference of non-template copy&move constructors and assignment operators:

template<typename T> class A {
public:
    A() { /* implementation here */ }

    // Remove from the overloads the default copy&move constructors and assignment operators
    A(const A&) = delete;
    A& operator=(const A&) = delete;
    A(A&&) = delete;
    A& operator=(A&&) = delete;

    // I want these to be used e.g. by std::vector
    template<typename U> A(const A<U>& fellow) { /* implementation here */ }
    template<typename U> A& operator=(const A<U>& fellow) { /* implementation here */ }

    template<typename U> A(A<U>&& fellow) { /* implementation here */ }
    template<typename U> A& operator=(A<U>&& fellow) { /* implementation here */ }        
};

但是,出现以下错误

试图引用已删除的功能

attempting to reference a deleted function

当试图将 A 项推到矢量或简单地复制构造时,例如:

when trying to push A items to a vector or simply copy-construct like:

A<int> a1{};
A<int> a2(a1);

UPDATE1:我需要模板copy& move构造函数和赋值运算符,因为template参数实际上仅控制某些缓存,因此可以安全地将 A< T1> 分配给 A< T2>..

UPDATE1: I need template copy&move constructors and assignment operators, because the template argument really just controls some caching, so A<T1> can be safely assigned to A<T2>.

推荐答案

您可以通过声明带有备用签名的已删除副本构造函数/赋值运算符来使编译器满意,这不会导致选择此重载,但会阻止生成构造函数/赋值编译器运算符:

You can make compiler happy by declaring deleted copy constructor / assignment operator with alternative signature which will not cause this overload to be selected but will prevent generation of constructor / assignment operator by compiler:

template<typename T> class A
{ public:
    A() { /* implementation here */ }

    // Remove from the implicit declaration of the default copy&move constructors and assignment operators
    A(A volatile const &) = delete;
    A & operator =(A volatile const &) = delete;

    // I want these to be used e.g. by std::vector
    template<typename U> A(A<U> const & fellow) { /* implementation here */ }
    template<typename U> A & operator =(A<U> const & fellow) { /* implementation here */ return *this;}

    template<typename U> A(A<U> && fellow) { /* implementation here */ }
    template<typename U> A & operator =(A<U> && fellow) { /* implementation here */ return *this; }        
};
int main()
{
    A<int> a1{};
    A<int> a2{a1};
    return 0;
}

在线编译器

15.8.1复制/移动构造函数[class.copy.ctor]
1.如果 class X 的非模板构造函数的第一个参数的类型为 X& const X& volatile X& const volatile X& ,或者没有其他参数,或者所有其他参数都具有默认参数

15.8.1 Copy/move constructors [class.copy.ctor]
1. A non-template constructor for class X is a copy constructor if its first parameter is of type X&, const X&, volatile X& or const volatile X&, and either there are no other parameters or else all other parameters have default arguments

这篇关于如何利用模板复制和移动构造函数和赋值运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆