c++ - 是否可以有一个默认的复制构造函数和一个模板化的转换构造函数?

标签 c++ templates constructor

如果我有这样的类(class)

template <typename T>
class MyClass
{
    T myData;

public:
    T getValue() { return myData; }        

    template <typename V>
    MyClass(const MyClass<V>& other) :
        myData((T) other.getValue())
    {
    }
};

这意味着我提供了一个复制构造函数(对于 V=T),因此根据此链接 Why no default move-assignment/move-constructor?我没有得到默认的移动构造函数等。

有没有办法让模板化构造函数只作为转换构造函数,所以对于 V!=T?

最佳答案

你的前提是错误的。构造函数模板从不用于实例化复制(或移动)构造函数。换句话说,复制/移动构造函数始终是非模板成员函数,即使成员函数模板可以生成具有适当复制/移动签名的构造函数。

照原样,除了模板之外,您的类仍将有一个普通的复制构造函数。

关于c++ - 是否可以有一个默认的复制构造函数和一个模板化的转换构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39016896/

相关文章:

c# - C++ C# 包装器相关。如何从 C# 窗口获取 hwnd 并将其传递给 C++?

java - 将帧(android 中的 mat 数据)从 android 传递到 native c++ 并检测人脸

c++ - 模板类成员函数特化

java - 无法理解 "Leaking the this reference"

c++ - 构造函数的调用顺序

c++ - 如果不从派生构造函数调用基类构造函数会怎样?

c++ - memmove 和 c++11 std::move 有什么区别?

c++ - [std::streampos] 可以隐式转换为 [size_t] 吗?

c++ - 如何解决这个不明确的模板构造函数调用?

c++ - 从模板函数调用非模板函数