8

テンプレート基本クラスで「using」宣言を使用することは可能ですか? 私はそれがここにないことを読みましたが、それは技術的な理由によるものですか、それとも C++ 標準に反するものですか? gcc または他のコンパイラに適用されますか? それが不可能な場合、それはなぜですか?

コード例 (上記のリンクから):

struct A {
    template<class T> void f(T);
};

struct B : A {
    using A::f<int>;
};
4

1 に答える 1

5

リンク先は using ディレクティブです。using 宣言は、テンプレート化された基本クラスで問題なく使用できます (標準で調べていませんが、コンパイラでテストしただけです)。

template<typename T> struct c1 { 
    void foo() { std::cout << "empty" << std::endl; } 
}; 

template<typename T> struct c2 : c1<T> { 
    using c1<T>::foo; 
    void foo(int) { std::cout << "int" << std::endl; } 
}; 

int main() { 
    c2<void> c;
    c.foo();
    c.foo(10); 
}

fooコンパイラは、using 宣言が のスコープ内で再宣言しているため、パラメータのない関数を正しく検出しc2、期待される結果を出力します。

編集:質問を更新しました。ここに更新された答えがあります:

template-id (テンプレート名と引数) の使用が許可されていないという記事は正しいです。ただし、テンプレート名を付けることができます:

struct c1 { 
    template<int> void foo() { std::cout << "empty" << std::endl; } 
}; 

struct c2 : c1 { 
    using c1::foo; // using c1::foo<10> is not valid
    void foo(int) { std::cout << "int" << std::endl; } 
}; 

int main() { 
    c2 c;
    c.foo<10>();
    c.foo(10); 
}
于 2008-12-06T20:15:37.757 に答える