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;
}

さて、 のときにコンパイルに失敗する理由はかなり明白BROKEN == 1ですが、 の中に非 const ポインターが必要であるという事実を回避するために物事を構造化する正しい方法を理解するのに苦労していますが、それでもしたいと思いWrapperますcbegin()およびの戻り値の型の 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)

ただし、コンストラクターでWrapper<const T, ...>a を使用することはできません。Tここに解決策があると思います。サブクラスが を受け取る場合は、 ;decltype(d)を渡す必要があります。decltype(d)解決策が正確に何であるかはわかりませんが、上記は誰かがそれを見つけるのに役立つかもしれません!

于 2013-03-08T22:45:20.777 に答える
0

次のように、Wrapper 内に 2 つのポインターを保持することを検討しますか。

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 に答える