这种重载分辨率头痛的原因是什么? [英] What is the cause of this overload resolution headache?
问题描述
我有一个程序,我有很多嵌套的if / switch语句,在几个地方重复。我试图提取出来,并把开关放在一个模板方法类,然后允许客户端重载他们想要使用重载特别处理的开关分支:
I've got a program where I've got a lot of nested if/switch statements which were repeated in several places. I tried to extract that out and put the switches in a template method class, and then allow clients to overload which switch branches they wanted to specifically handle using overloading:
class TraitsA {};
class TraitsB : public TraitsA {};
class Foo
{
bool traitsB;
public:
// Whether or not a Foo has traitsB is determined at runtime. It is a
// function of the input to the program and therefore cannot be moved to
// compile time traits (like the Iterators do)
Foo() : traitsB(false) {}
virtual ~Foo() {}
bool HasTraitsB() const { return traitsB; }
void SetTraitsB() { traitsB = true; }
};
class SpecificFoo : public Foo
{
};
template <typename Client> //CRTP
class MergeFoo
{
protected:
Foo DoMerge(Foo&, const Foo&, int, TraitsA)
{
// Do things to merge generic Foo
}
public:
// Merge is a template method that puts all the nasty switch statements
// in one place.
// Specific mergers implement overloads of DoMerge to specify their
// behavior...
Foo Merge(Foo* lhs, const Foo* rhs, int operation)
{
const Client& thisChild = *static_cast<const Client*>(this);
SpecificFoo* lhsSpecific = dynamic_cast<SpecificFoo*>(lhs);
const SpecificFoo* rhsSpecific = dynamic_cast<const SpecificFoo*>(rhs);
// In the real code these if's are significantly worse
if (lhsSpecific && rhsSpecific)
{
if (lhs->HasTraitsB())
{
return thisChild.DoMerge(*lhsSpecific,
*rhsSpecific,
operation,
TraitsB());
}
else
{
return thisChild.DoMerge(*lhsSpecific,
*rhsSpecific,
operation,
TraitsA());
}
}
else
{
if (lhs->HasTraitsB())
{
return thisChild.DoMerge(*lhs, *rhs, operation, TraitsB());
}
else
{
return thisChild.DoMerge(*lhs, *rhs, operation, TraitsA());
}
}
}
};
class ClientMergeFoo : public MergeFoo<ClientMergeFoo>
{
friend class MergeFoo<ClientMergeFoo>;
Foo DoMerge(SpecificFoo&, const SpecificFoo&, int, TraitsA)
{
// Do things for specific foo with traits A or traits B
}
};
class ClientMergeFooTwo : public MergeFoo<ClientMergeFoo>
{
friend class MergeFoo<ClientMergeFooTwo>;
Foo DoMerge(SpecificFoo&, const SpecificFoo&, int, TraitsB)
{
// Do things for specific foo with traits B only
}
Foo DoMerge(Foo&, const Foo&, int, TraitsA)
{
// Do things for specific foo with TraitsA, or for any Foo
}
};
但是,这无法编译(至少在 ClientMergeFooTwo
'case),说它不能转换Foo&转换为SpecificFoo&任何想法为什么它不能转换,而不是在 MergeFoo
?
However, this fails to compile (At least in ClientMergeFooTwo
's case), saying it cannot convert a Foo& into a SpecificFoo&. Any ideas why it's failing that conversion instead of choosing the perfectly good generic overload in MergeFoo
?
编辑中选择完美的通用重载:好吧,这个伪代码示例显然没有这么好,给定我有多快我试图写它。我已更正了一些错误...
EDIT: Well, this psuedocode example apparently didn't do so well given how fast I tried to write it. I have corrected some of the mistakes...
推荐答案
任何想法,在MergeFoo中选择完美的通用重载?
Any ideas why it's failing that conversion instead of choosing the perfectly good generic overload in MergeFoo?
是的,因为名称隐藏规则。如果派生类中的函数与基类中的函数具有相同的名称,则基类函数为隐藏,它甚至不查看所涉及函数的参数。
Yes, because of name hiding rules. If a function in the derived class has the same name as a function in the base class, the base class function is "hidden", it doesn't even look at the parameters of the involved function.
也就是说,解决方案很简单:使用一个简单的在公共部分使用MergeFoo :: DoMerge
在派生类中提供基类版本。
That said, the solution is easy: Make the base class version available in the derived class with a simple using MergeFoo::DoMerge
in the public part.
这篇关于这种重载分辨率头痛的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!