亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么功能模板不能部分專業化?

為什么功能模板不能部分專業化?

C++
MYYA 2019-12-25 15:41:47
我知道語言規范禁止對功能模板進行部分專業化。我想知道為什么它禁止這樣做的理由?它們沒有用嗎?template<typename T, typename U> void f() {}   //allowed!template<> void f<int, char>()            {}   //allowed!template<typename T> void f<char, T>()    {}   //not allowed!template<typename T> void f<T, int>()     {}   //not allowed!
查看完整描述

3 回答

?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

我想這只是一個疏忽(考慮到通過將函數作為static類的成員,您總是可以使用更詳細的代碼來獲得部分專業化效果)。


您可能會查找相關的DR(缺陷報告)(如果有)。


編輯:檢查這一點,我發現其他人也相信這一點,但是沒有人能夠在標準草案中找到任何這樣的支持。該SO線程似乎表明C ++ 0x不支持功能模板的部分專業化。


編輯2:只是我的意思是“將函數作為static類的成員放置”的一個示例:


#include <iostream>

using namespace std;


// template<typename T, typename U> void f() {}   //allowed!

// template<> void f<int, char>()            {}   //allowed!

// template<typename T> void f<char, T>()    {}   //not allowed!

// template<typename T> void f<T, int>()     {}   //not allowed!


void say( char const s[] ) { std::cout << s << std::endl; }


namespace detail {

    template< class T, class U >

    struct F {

        static void impl() { say( "1. primary template" ); }

    };


    template<>

    struct F<int, char> {

        static void impl() { say( "2. <int, char> explicit specialization" ); }

    };


    template< class T >

    struct F< char, T > {

        static void impl() { say( "3. <char, T> partial specialization" ); }

    };


    template< class T >

    struct F< T, int > {

        static void impl() { say( "4. <T, int> partial specialization" ); }

    };

}  // namespace detail


template< class T, class U >

void f() { detail::F<T, U>::impl(); }    


int main() {

    f<char const*, double>();       // 1

    f<int, char>();                 // 2

    f<char, double>();              // 3

    f<double, int>();               // 4

}


查看完整回答
反對 回復 2019-12-25
?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

好吧,您確實不能執行部分函數/方法專門化,但是可以執行重載。


template <typename T, typename U>

T fun(U pObj){...}


// acts like partial specialization <T, int> AFAIK 

// (based on Modern C++ Design by Alexandrescu)

template <typename T>

T fun(int pObj){...} 

是這樣,但我不知道它是否滿足您。


查看完整回答
反對 回復 2019-12-25
  • 3 回答
  • 0 關注
  • 521 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號