C++ C++;未调用向量元素构造函数

C++ C++;未调用向量元素构造函数,c++,c++11,stdvector,move-semantics,reference-counting,C++,C++11,Stdvector,Move Semantics,Reference Counting,我有一个带有复制构造函数和移动构造函数的类,这两个类都会向stdout报告消息,直到我解决了这个问题。将局部对象推送到向量上时,不会调用构造函数,这会导致以后的问题。但是,当我使用std::move告诉它使用move构造函数而不是copy构造函数时,一切都正常。这是一个bug,还是我误解了std::vector的工作原理 以下是我的对象的构造函数: template <typename R> inline Ref (const Ref<R> &other) : m

我有一个带有复制构造函数和移动构造函数的类,这两个类都会向stdout报告消息,直到我解决了这个问题。将局部对象推送到向量上时,不会调用构造函数,这会导致以后的问题。但是,当我使用std::move告诉它使用move构造函数而不是copy构造函数时,一切都正常。这是一个bug,还是我误解了std::vector的工作原理

以下是我的对象的构造函数:

template <typename R>
inline Ref (const Ref<R> &other)
: m_ptr(other.m_ptr)
{
  LogDebugc("copy ctor ", long(other.m_ptr));
  Retain(m_ptr);
}

template <typename R>
inline Ref (Ref<R> &&other)
: m_ptr(other.m_ptr)
{
  LogDebugc("move ctor ", long(other.m_ptr));
  other.m_ptr = nullptr;
}
…但是,相反,我得到了

[dbg] pre push 6415744
[dbg] post push 6415744
当我更改状态被推回的行时,我得到:

s_stateStack.push_back(std::move(ref));

[dbg] pre push 6415744
[dbg] move ctor 6415744
[dbg] post push 6415744
这让我非常困惑。

模板
template <typename R>
inline Ref (const Ref<R> &other)
: m_ptr(other.m_ptr)
{
  LogDebugc("copy ctor ", long(other.m_ptr));
  Retain(m_ptr);
}
内联参考(常数参考和其他) :m_ptr(其他.m_ptr) { LogDebugc(“复制向量”,long(other.m_ptr)); 保留(m_ptr); }
这不是复制构造函数。因此,它没有被称为

§12.8如果类X的非模板构造函数的第一个参数类型为X&、常量X&、volatile X&或常量volatile X&,并且没有其他参数,或者所有其他参数都有默认参数,则类X的构造函数为副本构造函数


编译器使用的是隐式生成的复制构造函数,而不是您编写的转换构造函数。

retain做什么,这是您的两个函数之间的区别。您的问题涉及向量,但我看不到任何向量。是否有可能将向量上的“U”推回会逐个成员分配?与中一样,它调用运算符=(),而不是复制构造函数?@Daggerbot:将声明放在一个代码区域中,在该区域中,
*
&
不会被GRUE吃掉。刚刚意识到这一点。但让我们从现有的十几个问题中找出一个,好吗?事实证明这就是问题所在。修正!:D
s_stateStack.push_back(std::move(ref));

[dbg] pre push 6415744
[dbg] move ctor 6415744
[dbg] post push 6415744
template <typename R>
inline Ref (const Ref<R> &other)
: m_ptr(other.m_ptr)
{
  LogDebugc("copy ctor ", long(other.m_ptr));
  Retain(m_ptr);
}