使用一些C ++模板包装器代码将自己绘制到const-corner中

时间:2013-03-08 21:40:05

标签: c++ templates stl const

我正在研究用于序列化数据的模板化框架,并且在我的代码中遇到了一些涉及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;
}

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

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

提前致谢。

2 个答案:

答案 0 :(得分: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)

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

答案 1 :(得分: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()const,则会出现NULL指针错误,这当然是错误的。但无论如何都要打电话给他们是不好的。