模板复制构造函数 [英] template copy constructor

查看:247
本文介绍了模板复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定下面的代码,Foo有复制构造函数吗?用STL容器使用Foo是否安全?

  class Foo 
{
public:
Foo(){}

template< typename T>
Foo(const T&){}
};


解决方案

标准明确地说复制构造函数是一个非模板化的构造函数,它引用一个可能的相同类型的const volatile对象。在上面的代码中,您有一个转换而不是复制构造函数(即,它将用于所有


是的,隐式声明/定义的拷贝构造函数。


使用标准库容器安全使用 Foo 吗?


当前定义 Foo 是,但在一般情况下,它取决于成员 Foo 是否具有隐式定义的复制构造函数正确地管理它们。


Given the following code, does Foo have copy constructor? Is it safe to use Foo with STL containers?

class Foo
{
public:
   Foo() {}

   template <typename T>
   Foo(const T&) {}   
};

解决方案

The standard explicitly says that a copy constructor is a non-templated constructor that takes a reference to a possibly const-volatile object of the same type. In the code above you have a conversion but not copy constructor (i.e. it will be used for everything but copies, where the implicitly declared constructor will be used).

Does Foo have a copy constructor?

Yes, the implicitly declared/defined copy constructor.

Is it safe to use Foo with standard library containers?

With the current definition of Foo it is, but in the general case, it depends on what members Foo has and whether the implicitly defined copy constructor manages those correctly.

这篇关于模板复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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