[英]Why can't I call a templated method from a derived class instance in C++?
[英]Why can't I call a templated method of a template class that is derived from
struct Messages
{
template <typename V>
static const char* message() {return "test mesage";}
};
template <int Min, class M=Messages>
struct Test: public M
{
Test()
{
M::message<int>(); //error: expected primary-expression before 'int'
}
};
int main()
{
Test<5, Messages> t;
}
我懷疑這與一些相互依賴有關,比如Test的代碼依賴於基類M,其方法在Test中是專用的。 它是否正確?
M::message
是一個從屬名稱,因為M
是一個模板參數。 編譯器無法知道從屬名稱本身就是模板,因此您需要明確指定:
M::template message<int>();
否則,編譯器會解析代碼,就像M::message
是一個值一樣,它給出了以下尖括號不同的含義(即它們被解析為小於和大於運算符而不是模板列表分隔符)。 編譯器無法從這種錯誤的解析中恢復。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.