我在 T
类型的对象上有很多有用的函数。这里 T
需要为使用它的函数提供一些接口(interface)。该接口(interface)有几种常见的实现。所以我使用 CRTP 使它们作为 mixins 工作。
template<class T>
struct InterfaceImpl {
using ImplType = InterfaceImpl<T>;
int foo();
...
};
struct MyData : public InterfaceImpl<MyData> {
...
};
template<class T>
void aUsefulFunction(T& t) {
//Working with `t`.
//This cast is to workaround an accidental hiding of `foo` by MyData.
static_cast<T::ImplType&>(t).foo();
}
出于某种原因,我希望按原样提供实现 InterfaceImpl
(以及其他实现)。隐藏他们的一些方法可能非常危险。他们有什么办法可以强制不让子类(class)覆盖吗?我读了link关于类似的问题,但讨论并没有给出令人满意的解决方案。如果没有合理的方法,我希望上面代码中的转换可以提供一些安全性。或者有没有其他办法解决这个问题?
最佳答案
您可以创建一个特征来查看 T
是否具有 foo
并在其上使用 static_assert
:
typename <typename T, typename ...Ts>
using foo_type = decltype(std::declval<T>().foo(std::declval<Ts>()...));
template <typename T>
using has_foo = is_detected<foo_type, T>;
template<class T>
struct InterfaceImpl {
static_assert(!has_foo<T>::value, "T should not have foo method");
using ImplType = InterfaceImpl<T>;
int foo();
};
MyData
仍然可以使用 MyData::foo(int)
或类似的方法隐藏 foo
,但是如果调用错误的方法。
关于c++ - 防止意外隐藏(由 CRTP mixin 提供的方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44879955/