1

この非常に漠然としたタイトルで申し訳ありませんが、説明するのは非常に難しいです。

私が立ち往生しているエラーはこれです、私はそれが何を意味するのか分かりません:

carray.h:176:エラー:'typename Carray <T、Allocator> :: is_iterator' names'template <class T、class Allocator> template <class I、bool check> struct Carray <T、Allocator> :: is_iterator'、タイプではありません

何かがイテレーターであるかどうかを検出し、正しいオーバーロードを使用するためのこのスニペットがあります(http://stackoverflow.com/questions/6050441/why-does-this-constructor-overload-resolve-incorrectly)。これはコンパイルします:

template<class T> class Carray {
private:
    // uses SFINAE to determine if the passed in type is indeed an iterator
    template<class It>
    class is_iterator_impl {
    private:
        typedef char yes[1];
        typedef char no[2];

        template<class C>
        static yes& _test(typename C::iterator_category*);

        template<class>
        static no& _test(...);
    public:
        static const bool value = sizeof(_test<It>(0)) == sizeof(yes);
    };

    template<class It, bool check = is_iterator_impl<It>::value> struct is_iterator { typedef void type; };
    template<class It> struct is_iterator<It*, false> { typedef void type; };
    template<class It> struct is_iterator<It, false> { };

public:
    template<class It>
    Carray(It first, It last, typename is_iterator<It>::type *dummy = 0) {
        // create array from 2 iterators
    }
};

ここで、実装を宣言から分離したかったので、これを試しましたが、エラーが発生しました。

template<class T> class Carray {
private:
    // uses SFINAE to determine if the passed in type is indeed an iterator
    template<class It> class is_iterator_impl;
    template<class It, bool check = is_iterator_impl<It>::value> struct is_iterator { typedef void type; };
    template<class It> struct is_iterator<It*, false> { typedef void type; };
    template<class It> struct is_iterator<It, false> { };
public:
    template<class It> Carray(It first, It last, typename is_iterator<It>::type *dummy = 0);
};

template<class T>
template<class It>
Carray<T>::Carray(It first, It last, typename Carray<T>::is_iterator<It>::type *dummy) {
    // create array from 2 iterators - ERROR IN THIS DEFINITION
}

template<class T>
template<class It>
class Carray<T>::is_iterator_impl {
private:
    typedef char yes[1];
    typedef char no[2];

    template<class C>
    static yes& _test(typename C::iterator_category*);

    template<class>
    static no& _test(...);
public:
    static const bool value = sizeof(_test<It>(0)) == sizeof(yes);
};

g++4.5.5を使用しています。

4

2 に答える 2

3

どういうわけかあいまいなこれらのタイプの問題(つまり、かなりのコードがあり、最初のパスで読むのは簡単ではありません)では、機能する(または失敗する例)を提供する必要があります。

私の推測では、templateキーワード(Carrayコンストラクター引数)が欠落しています:

typename Carray<T, Allocator>::template is_iterator<InputIterator>::type
//                             ^^^^^^^^
于 2011-06-18T17:52:05.130 に答える
-2

すべての行の終わりにあるセミコロンを確認してください。次のように書いたようです。

template<class T, class Allocator> 
template<class I, bool check> struct Carray<T, Allocator>::is_iterator

最初の行の終わりにセミコロンなしで、ただの推測です。

于 2011-06-18T17:35:53.073 に答える