![](/img/trans.png)
[英]C++ 17 How to specialize static function template in class template
[英]How to specialize a static function of a class template?
當我嘗試寫這樣的東西時:
#include <iostream>
template <class T>
class A
{
public:
static void doit();
};
template <>
static void A<int>::doit()
{
std::cout << "int" << std::endl;
}
template <>
static void A<double>::doit()
{
std::cout << "double" << std::endl;
}
int main()
{
A<int>::doit();
A<double>::doit();
}
全班專業都可以。 我只想知道有什么方法可以專門化靜態函數?
在聲明中,您只能指定一次static
關鍵字。
嘗試這個:
template<>
void A<int>::doit()
{
std::cout << "int" << std::endl;
}
template<>
void A<double>::doit()
{
std::cout << "double" << std::endl;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.