簡體   English   中英

如何專門化類模板的靜態功能?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM