2017-04-11 21 views
1

我的嘗試是:如何在CRTP中調用派生的模板化函數?

template<typename Derived> 
struct Base 
{ 
    void A() 
    { 
     ((Derived *)this)->B<42>(); 
    } 
}; 

struct Derived : Base<Derived> 
{ 
    template<int> void B() { } 
}; 

http://coliru.stacked-crooked.com/a/cb24dd811b562466

導致

main.cpp: In member function 'void Base<Derived>::A()': 
main.cpp:6:34: error: expected primary-expression before ')' token 
     ((Derived *)this)->B<42>(); 
           ^
main.cpp: In instantiation of 'void Base<Derived>::A() [with Derived = Derived]': 
main.cpp:17:17: required from here 
main.cpp:6:30: error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<' 
     ((Derived *)this)->B<42>(); 
     ~~~~~~~~~~~~~~~~~~~~^~~ 
+0

編譯器錯誤? VS 2017工作正常 – Raxvan

+0

@Raxvan MSVC不執行兩階段查找,這是部分原因在那裏工作的部分原因。不是編譯器錯誤。 – user975989

回答

2

您需要keyword template調用上的依賴型的模板函數:

((Derived *)this)->template B<42>(); 
//     ~~~~~~~~ 

在模板定義中,可以使用template來聲明從屬名稱是模板。

相關問題