2014-02-26 12 views
2

我正在使用CRTP並且基類具有模板函數。我如何use成員函數在模板派生類?「使用」指令如何與模板成員函數一起工作

template <typename T> 
struct A { 
    int f(); 
    template <typename S> 
    int g(); 
}; 
struct B: public A<B> { 
    int h() { return f() + g<void>(); } // ok 
}; 
template <typename T> 
struct C: public A<C<T>> { 
    // must 'use' to get without qualifying with this-> 
    using A<C<T>>::f; // ok 
    using A<C<T>>::g; // nope 
    int h() { return f() + g<void>(); } // doesn't work 
}; 

*編輯* 前面一個問題,Using declaration for type-dependent template name,包括評論,表明這是不可能的,可能是在標準的監督。

回答

1

我不知道如何解決using聲明(它應該看起來像using A<C<T>>::template g;,但這個代碼不能用我的編譯器編譯)的問題。但是,你可以調用g<void>方法在下列方式之一:

  • this->template g<void>()

  • A<C<T>>::template g<void>()

查看答案this question關於使用template關鍵字的陰暗面細節。

+0

是的,你的另外兩條語句是我如何在不使用'使用'的情況下訪問g,但它們都非常麻煩!我希望'模板'的魔法咒語能讓我避開它們。 –

相關問題