用于提供正确的类似指针的行为(指向常量的指针)的替代类设计?
我正在编写一组 C++ 参数化类,我对其中一些与指针类似的行为感兴趣。特别是,我希望能够从具有非常量模板参数的对象创建具有常量模板参数的对象,但反之则不然。这个示例代码应该澄清我的意图:
int main() {
myClass<int> mc_int;
myClass<const int> mc_const_int;
myClass<const int> mc1(mc_const_int); // This should compile.
myClass<int> mc2(mc_int); // This should compile.
myClass<const int> mc3(mc_int); // This should compile.
myClass<int> mc4(mc_const_int); // This should NOT compile.
}
我已经能够通过创建下一个类层次结构(为了可读性而简化)来实现这个特定的行为:
template <typename T>
class Base {
// ...
protected:
template <typename U>
Base(const Base<U> &obj): _elem(obj._elem) {}
private:
T _elem;
friend class Base<const T>;
};
template <typename T>
class myClass: public Base<T> {
// ...
public:
template <typename U>
myClass(const myClass<U> &obj): Base<const U>(obj) {}
};
并且它按预期工作,但我对这个设计并不完全满意,因为我只能检测来自构造函数的非常量模板参数,但不来自任何其他成员函数。
例如,如果我想创建一个带有 addAll()
方法的容器类,我希望能够做到这一点:
int main() {
Container<int> c_int;
c_int.add(new int(1));
c_int.add(new int(2));
c_int.add(new int(3));
Container<const int> c_const_int;
c_const_int.addAll(c_int); // This should compile.
c_int.addAll(c_const_int); // This should NOT compile.
}
但我不知道如何实现前面的行为。有谁有替代设计的想法来实现我想要做的事情?有谁知道更深入讨论这个问题的链接?
提前致谢。
I'm writing a set of C++ parameterized classes and I'm interested in some of them behaving similarly to pointers. In particular, I want to be able to create an object with a constant template parameter from an object with a non-constant template parameter, but not the other way around. This sample code should clarify my intentions:
int main() {
myClass<int> mc_int;
myClass<const int> mc_const_int;
myClass<const int> mc1(mc_const_int); // This should compile.
myClass<int> mc2(mc_int); // This should compile.
myClass<const int> mc3(mc_int); // This should compile.
myClass<int> mc4(mc_const_int); // This should NOT compile.
}
I have been able to achieve this particular behavior by creating the next class hierarchy (simplified for readability):
template <typename T>
class Base {
// ...
protected:
template <typename U>
Base(const Base<U> &obj): _elem(obj._elem) {}
private:
T _elem;
friend class Base<const T>;
};
template <typename T>
class myClass: public Base<T> {
// ...
public:
template <typename U>
myClass(const myClass<U> &obj): Base<const U>(obj) {}
};
And it works as expected, but I'm not entirely satisfied with this design because I can only detect a non-constant template parameter from the constructor, but not from any other member function.
If I wanted, for example, to create a container class with an addAll()
method, I would like to be able to do this:
int main() {
Container<int> c_int;
c_int.add(new int(1));
c_int.add(new int(2));
c_int.add(new int(3));
Container<const int> c_const_int;
c_const_int.addAll(c_int); // This should compile.
c_int.addAll(c_const_int); // This should NOT compile.
}
But I don't know how to achieve the previous behavior. Does anyone have ideas for an alternate design to achieve what I'm trying to do? Does anyone know of a link where this problem is discussed in more depth?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一种可能性是使用成员函数模板,使它们能够实现您想要的组合。
One possibility is using member function templates, enabling them so as to achieve the combinations you want.
实现此目的的一种方法是通过部分模板专业化。您应该像平常一样为非 const 类型定义类:
然后定义一个专门化
不幸的是,这会导致代码重复,但它应该允许您做您想做的事情。当然,您也可以让函数采用
MyClass&
和MyClass&
。One way to do this is via partial template specialisation. You should define the class for non-
const
types as you usually would:Then define a specialisation
Unfortunately, this leads to code duplication, but it should allow you to do what you want. Naturally, you can have the functions take
MyClass<T>&
andMyClass<T const>&
, too.我有点理解这个问题了。您可能可以使用模板专业化。
I'm kind of understand the question a bit. You probably can use template specialization.