所以我有这样的代码:为什么有一个复制构造函数会导致此代码中断?
template<class T>
struct IntegerType
{
T value;
//Next line causes errors
//constexpr IntegerType(IntegerType& value) : value(value.value) { }
constexpr IntegerType(T value) : value(value) { }
};
template<class int_t>
class FullMult{
int_t value;
int_t carry;
public:
constexpr FullMult() : value(0), carry(0) { }
constexpr FullMult(const int_t& value, const int_t& carry) : value(value), carry(carry) { }
};
int main()
{
FullMult<IntegerType<unsigned int> > thing
= FullMult<IntegerType<unsigned int> >(
IntegerType<unsigned int>(12),IntegerType<unsigned int>(12));
}
但是当我尝试通过取消注释行constexpr IntegerType(IntegerType& value) : value(value.value) { }
代码休息,以一个拷贝构造函数添加到类型IntegerType
并告诉我,我想使用的拷贝构造FullMult
类型:
use of deleted function 'FullMult<IntegerType<unsigned int> >::FullMult(FullMult<IntegerType<unsigned int> >&&)'
这是给我的错误代码:
template<class T>
struct IntegerType
{
T value;
//Next line causes errors
constexpr IntegerType(IntegerType& value) : value(value.value) { }
constexpr IntegerType(T value) : value(value) { }
};
template<class int_t>
class FullMult{
int_t value;
int_t carry;
public:
constexpr FullMult() : value(0), carry(0) { }
constexpr FullMult(const int_t& value, const int_t& carry) : value(value), carry(carry) { }
};
int main()
{
FullMult<IntegerType<unsigned int> > thing
= FullMult<IntegerType<unsigned int> >(
IntegerType<unsigned int>(12),IntegerType<unsigned int>(12));
}
这里发生了什么事?
您是否使用一个类作为另一个类的模板参数? – Kupto
问题中的代码应该是破碎的代码,而不是工作代码。请清楚明确地发布一个显示问题的MCVE –
@Kupto是..... – DarthRubik