2

我正在开发一个用于序列化数据的模板化框架,并且在我的代码中遇到了一些涉及 const-ness 保证的障碍。我找到了一些解决 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;
}

现在,很明显为什么事情无法编译 when BROKEN == 1,但我很难找出构建事物的正确方法来解决我想要一个非常量指针的事实 a Wrapper,但仍然想保留cbegin()and的返回类型的 const 语义cend()。我已经尝试了一些事情,包括创建一个const_Wrapper具有不同语义的单独类,但是这些包装类经常遇到模板解析问题。

如果有人可以建议(1)一种更好的方法来做这种事情,或者(2)指出一些做类似事情的代码的方向,我真的很感激。请记住,从这个示例中看不到围绕底层容器创建包装器的原因(因为真正的包装器类继承了一个用于读取和写入的接口)。

提前致谢。

4

2 回答 2

2

这个问题可以简化一点

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)

但是 aWrapper<const T, ...>不能T在构造函数中使用 a 。我认为这是可以找到解决方案的地方;如果您的子类采用 a decltype(d),则需要将其传递 a decltype(d);不确定解决方案到底是什么,但也许以上内容可以帮助某人发现它!

于 2013-03-08T22:45:20.777 回答
0

您是否考虑在 Wrapper 中保留两个指针,如下所示:

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), const_container(&c) { }
    Wrapper(const C & c) : container(NULL), const_container(&c) { }

    // Copy constructor
    Wrapper(const Wrapper & c) = delete; // violates const-ness

    Wrapper(Wrapper & c) : container(c.container), const_container(c.const_container) { }

    // Iterator access
    iterator begin() { return iterator(container->begin()); }
    iterator end() { return iterator(container->end()); }

    const_iterator cbegin() const
        { return const_iterator(const_container->cbegin()); }
    const_iterator cend() const
        { return const_iterator(const_container->cend()); }

    // Accessor functions
    size_t length() const { return const_container->size(); }

 protected:
    C * container;
    const C * const_container;
};

如果您尝试调用begin()end()在使用引用构造的包装器上,则会出现 NULL 指针错误const,这当然是不好的。但无论如何打电话给他们是不好的。

于 2013-03-08T23:03:08.373 回答