我正在使用Visual Studio 2010编译器看到我认为是错误/不正确的编译器错误。我正在从Visual Studio 2005向上移植我们的代码库,并且我遇到了之前正在构建的构造,但现在生成了C2248编译器错误。
显然,下面的代码片段已经通用化,但它是该场景的可编辑示例。 ObjectPtr<T>
C ++模板来自我们的代码库,并且是有问题的错误的来源。似乎正在发生的事情是编译器正在生成对ObjectPtr<T>
的复制构造函数的调用(如果不应该这样)(请参阅下面的SomeContainer::Foo()
方法中的注释块)。对于此代码构造,SomeUsefulData *
上有ObjectPtr<SomeUsefulData>
的公共强制转换运算符,但如果?:
运算符未在 true 表达式中选择它。相反,我在下面的块引用中得到了两个错误。
根据我对C ++的了解,这段代码应该编译。有没有人见过这种行为?如果没有,有人可以指出我对编译器解析规则的澄清,这可以解释为什么它在这种情况下试图生成对象的副本吗?
提前致谢,
迪伦布尔克
Visual Studio构建输出:
c:\ projects \ objectptrtest \ objectptrtest.cpp(177):错误C2248:'ObjectPtr :: ObjectPtr':无法访问类'ObjectPtr'中声明的私有成员
与
[T = SomeUsefulData]
c:\ projects \ objectptrtest \ objectptrtest.cpp(25):参见'ObjectPtr :: ObjectPtr'的声明
与
[T = SomeUsefulData]
c:\ projects \ objectptrtest \ objectptrtest.cpp(177):错误C2248:'ObjectPtr :: ObjectPtr':无法访问类'ObjectPtr'中声明的私有成员
与
[T = SomeUsefulData]
c:\ projects \ objectptrtest \ objectptrtest.cpp(25):参见'ObjectPtr :: ObjectPtr'的声明
与
[T = SomeUsefulData]
以下是该场景的最小可编辑示例:
#include <stdio.h>
#include <tchar.h>
template<class T>
class ObjectPtr {
public:
ObjectPtr<T> (T* pObj = NULL, bool bShared = false) :
m_pObject(pObj), m_bObjectShared(bShared)
{}
~ObjectPtr<T> ()
{
Detach();
}
private:
// private, unimplemented copy constructor and assignment operator
// to guarantee that ObjectPtr<T> objects are not copied
ObjectPtr<T> (const ObjectPtr<T>&);
ObjectPtr<T>& operator = (const ObjectPtr<T>&);
public:
T * GetObject ()
{ return m_pObject; }
const T * GetObject () const
{ return m_pObject; }
bool HasObject () const
{ return (GetObject()!=NULL); }
bool IsObjectShared () const
{ return m_bObjectShared; }
void ObjectShared (bool bShared)
{ m_bObjectShared = bShared; }
bool IsNull () const
{ return !HasObject(); }
void Attach (T* pObj, bool bShared = false)
{
Detach();
if (pObj != NULL) {
m_pObject = pObj;
m_bObjectShared = bShared;
}
}
void Detach (T** ppObject = NULL)
{
if (ppObject != NULL) {
*ppObject = m_pObject;
m_pObject = NULL;
m_bObjectShared = false;
}
else {
if (HasObject()) {
if (!IsObjectShared())
delete m_pObject;
m_pObject = NULL;
m_bObjectShared = false;
}
}
}
void Detach (bool bDeleteIfNotShared)
{
if (HasObject()) {
if (bDeleteIfNotShared && !IsObjectShared())
delete m_pObject;
m_pObject = NULL;
m_bObjectShared = false;
}
}
bool IsEqualTo (const T * pOther) const
{ return (GetObject() == pOther); }
public:
T * operator -> ()
{ ASSERT(HasObject()); return m_pObject; }
const T * operator -> () const
{ ASSERT(HasObject()); return m_pObject; }
T & operator * ()
{ ASSERT(HasObject()); return *m_pObject; }
const T & operator * () const
{ ASSERT(HasObject()); return (const C &)(*m_pObject); }
operator T * ()
{ return m_pObject; }
operator const T * () const
{ return m_pObject; }
operator bool() const
{ return (m_pObject!=NULL); }
ObjectPtr<T>& operator = (T * pObj)
{ Attach(pObj, false); return *this; }
bool operator == (const T * pOther) const
{ return IsEqualTo(pOther); }
bool operator == (T * pOther) const
{ return IsEqualTo(pOther); }
bool operator != (const T * pOther) const
{ return !IsEqualTo(pOther); }
bool operator != (T * pOther) const
{ return !IsEqualTo(pOther); }
bool operator == (const ObjectPtr<T>& other) const
{ return IsEqualTo(other.GetObject()); }
bool operator != (const ObjectPtr<T>& other) const
{ return !IsEqualTo(other.GetObject()); }
bool operator == (int pv) const
{ return (pv==NULL)? IsNull() : (LPVOID(m_pObject)==LPVOID(pv)); }
bool operator != (int pv) const
{ return !(*this == pv); }
private:
T * m_pObject;
bool m_bObjectShared;
};
// Some concrete type that holds useful data
class SomeUsefulData {
public:
SomeUsefulData () {}
~SomeUsefulData () {}
};
// Some concrete type that holds a heap-allocated instance of
// SomeUsefulData
class SomeContainer {
public:
SomeContainer (SomeUsefulData* pUsefulData)
{
m_pData = pUsefulData;
}
~SomeContainer ()
{
// nothing to do here
}
public:
bool EvaluateSomeCondition ()
{
// fake condition check to give us an expression
// to use in ?: operator below
return true;
}
SomeUsefulData* Foo ()
{
// this usage of the ?: operator generates a C2248
// error b/c it's attempting to call the copy
// constructor on ObjectPtr<T>
return EvaluateSomeCondition() ? m_pData : NULL;
/**********[ DISCUSSION ]**********
The following equivalent constructs compile
w/out error and behave correctly:
(1) explicit cast to SomeUsefulData* as a comiler hint
return EvaluateSomeCondition() ? (SomeUsefulData *)m_pData : NULL;
(2) if/else instead of ?:
if (EvaluateSomeCondition())
return m_pData;
else
return NULL;
(3) skip the condition check and return m_pData as a
SomeUsefulData* directly
return m_pData;
**********[ END DISCUSSION ]**********/
}
private:
ObjectPtr<SomeUsefulData> m_pData;
};
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
答案 0 :(得分:2)
构造函数和析构函数不应该具有类的模板参数:
ObjectPtr(T* pObj = NULL, bool bShared = false) :
m_pObject(pObj), m_bObjectShared(bShared)
{}
(注意缺少<T>
)
但我认为这是无关紧要的。请参阅下面的答案......
答案 1 :(得分:1)
我没有C ++标准的实际副本,但是从this draft,第102-103页开始,如果expression ? E1 : E2
类型为T1和T2,则语句格式不正确,如果T1和T2没有继承关系,并且一个是右值,则
使用此过程,确定 第二个操作数是否可以 转换为匹配第三个操作数, 以及第三个操作数是否可以 转换为匹配第二个操作数。 如果两者都可以转换,或者可以转换 转换但转换是 暧昧,该计划是不正确的。 如果两者都不能转换,那么 操作数保持不变 进一步检查是作为 如下面所描述的。如果只是一个 转换是可能的,那 转换适用于所选择的 操作数和转换后的操作数是 用来代替原始操作数 对于本节的其余部分。
这似乎表明你的陈述是不正确的(因为你有ObjectPtr
以及operator T*
的非显式构造函数,但正如我所说,我没有实际标准。