我正在开发一个用于序列化数据的模板化框架,但在我的代码中遇到了一些涉及常量保证的问题。我找到了几种解决 const 问题的方法(使用 const_cast
和其他更肮脏的技术),但我想我应该在这里问这个问题。
下面是一个微不足道的例子,我可以将事情归结为......
#include <deque>
#include <iostream>
#include <string>
#include <memory>
using namespace std; // bad form, but for simplicity
// Forward decl's
template <typename C, typename SubC> class Wrapper;
template <typename C, typename SubC>
struct w_iterator
{
typedef w_iterator _Self;
typedef SubC & _Ref;
typedef SubC * _Ptr;
typedef typename C::iterator internal_iter;
// Default c-tors
w_iterator() = delete;
w_iterator(const internal_iter & i) : internal(i) { }
// Copy c-tor
w_iterator(const w_iterator & i) : internal(i.internal) { }
// Dereference operators - the implementation of these is
// hackish and and awful, its done this way purely to create
// a simplified example
_Ref operator * () const
{ proxy.reset(new SubC(*internal)); return *proxy; }
_Ptr operator -> () const
{ proxy.reset(new SubC(*internal)); return proxy.get(); }
// Comparison
bool operator == (const _Self & i)
{ return (internal == i.internal); }
bool operator != (const _Self & i)
{ return (internal != i.internal); }
// Manipulation
_Self & operator ++ () { ++internal; return *this; }
// This is a hack to simplify the example, my real code uses a different mechanism
// for the temporary storage of the return type for the dereference objects
mutable std::unique_ptr<SubC> proxy;
internal_iter internal;
};
template <typename C, typename SubC>
struct const_w_iterator
{
typedef w_iterator<C,SubC> _nonConstSelf;
typedef const_w_iterator _Self;
typedef const SubC & _Ref;
typedef const SubC * _Ptr;
typedef typename C::iterator nonconst_internal_iter;
typedef typename C::const_iterator internal_iter;
const_w_iterator(const nonconst_internal_iter & i) : internal(i) { }
const_w_iterator(const internal_iter & i) : internal(i) { }
// Dereference
_Ref operator * () const
{ proxy.reset(new SubC(*internal)); return *proxy; }
_Ptr operator -> () const
{ proxy.reset(new SubC(*internal)); return proxy.get(); }
// Comparison
bool operator == (const _Self & i)
{ return (internal == i.internal); }
bool operator == (const _nonConstSelf & i)
{ return (internal == i.internal); }
bool operator != (const _Self & i)
{ return (internal != i.internal); }
bool operator != (const _nonConstSelf & i)
{ return (internal != i.internal); }
// Manipulation
_Self & operator ++ () { ++internal; return *this; }
// This is a hack to simplify the example, my real code uses a different mechanism
// for the temporary storage of the return type for the dereference objects
mutable std::unique_ptr<const SubC> proxy;
internal_iter internal;
};
template <typename C, typename SubC>
class Wrapper
{
public:
// Typedefs - shared btw here & const_Wrapper
typedef w_iterator<C, SubC> iterator;
typedef const_w_iterator<C, SubC> const_iterator;
// Default construction - only allow construction with an underlying
// container
Wrapper(C & c) : container(&c) { }
// Copy constructor
Wrapper(const Wrapper & c) = delete; // violates const-ness
Wrapper(Wrapper & c) : container(c.container) { }
// Iterator access
iterator begin() { return iterator(container->begin()); }
iterator end() { return iterator(container->end()); }
const_iterator cbegin() const
{ return const_iterator(container->cbegin()); }
const_iterator cend() const
{ return const_iterator(container->cend()); }
// Accessor functions
size_t length() const { return container->size(); }
protected:
C * container;
};
#define BROKEN 1
int main(int argc, char ** argv)
{
deque<string> d = { "one", "two", "three" };
deque<decltype(d)> dd = { d };
deque<decltype(dd)> ddd { dd };
cout << d.size() << " " << dd.size() << " " << ddd.size() << endl;
Wrapper<decltype(d), string> w(d);
Wrapper<decltype(dd), decltype(w)> ww(dd);
Wrapper<decltype(ddd), decltype(ww)> www(ddd);
for (auto i = www.begin(); i != www.end(); ++i)
{
cout << "www: " << i->length() << endl;
#if BROKEN
// produces the error: no matching function for call to
// 'Wrapper<deque<string>, string>::Wrapper(const deque<string>&)'
for (auto j = i->cbegin(); j != i->cend(); ++j)
#else
for (auto j = i->begin(); j != i->end(); ++j)
#endif
{
cout << "ww: " << j->length() << endl;
for (auto k = j->cbegin(); k != j->cend(); ++k)
cout << *k << endl;
}
}
return 0;
}
现在,很明显为什么当 BROKEN == 1
时编译失败。 ,但我很难找出构建事物的正确方法,以避免我想要在 Wrapper
内部有一个非常量指针的事实,但仍然希望保留 cbegin()
的返回类型的 const 语义。和cend()
。我尝试了一些方法,包括创建一个单独的 const_Wrapper
具有不同语义的类,但这些包装类不断遇到模板解析问题。
如果有人可以建议(1)一种更好的方法来完成此类事情,或者(2)为我指出一些执行类似操作的代码的方向,我将非常感激。请记住,在围绕底层容器创建包装器的示例中,有一个不明显的原因(因为真正的包装器类继承了用于读取和写入的接口(interface))。
提前致谢。
最佳答案
问题可以稍微简化为
const_w_iterator<decltype(dd), decltype(w)> itr = ww.cbegin();
*itr;
问题是对 proxy.reset(new SubC(*internal));
的调用,在本例中相当于:
proxy.reset(new Wrapper<decltype(d), string>(*internal))
// ^
//const decltype(d)
但是一个Wrapper<const T, ...>
不能接受T
在构造函数中。我认为这就是可以找到解决方案的地方;如果你的子类采用 decltype(d)
,您需要将 decltype(d)
传递给它;不确定解决方案到底是什么,但也许上面的内容可以帮助别人发现它!
关于c++ - 使用一些 C++ 模板包装器代码将自己陷入 const 角落,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15303790/