-1

この設定では:

template<int N>
struct Base {
    void foo();
};

class Derived : Base<1> {
    static void bar(Derived *d) {
        //No syntax errors here
        d->Base<1>::foo();
    }
};

すべてが正常に動作します。ただし、この例では:

template<class E>
struct Base {
    void foo();
};

template<class E>
class Derived : Base<E> {
    static void bar(Derived<E> *d) {
        //syntax errors here
        d->Base<E>::foo();
    }
};

私は得る:

error: expected primary-expression before '>' token
error: '::foo' has not been declared

違いは何ですか?2番目の原因で構文エラーが発生するのはなぜですか?

4

1 に答える 1

1

コードが Clang 3.2 (こちらを参照) および GCC 4.7.2 (こちらを参照) で正常にコンパイルされるという前提で、Base<E>::使用する理由がわかりませんd->foo()

template<class E>
struct Base {
    void foo() { }
};

template<class E>
struct Derived : Base<E> {
    static void bar(Derived<E> *d) {
        //syntax errors here
        d->foo();
    }
};

int main()
{
    Derived<int> d;
    Derived<int>::bar(&d);
}

templateまたは、曖昧さ回避ツールを使用することもできます。

d->template Base<E>::foo();
于 2013-03-04T10:43:50.930 に答える