久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

當(dāng)模板參數(shù)用作另一個(gè)模板的模板參數(shù)時(shí),為什

Why can#39;t the template argument be deduced when it is used as template parameter to another template?(當(dāng)模板參數(shù)用作另一個(gè)模板的模板參數(shù)時(shí),為什么不能推導(dǎo)出模板參數(shù)?) - IT屋-程序員軟件開(kāi)發(fā)技術(shù)分享社
本文介紹了當(dāng)模板參數(shù)用作另一個(gè)模板的模板參數(shù)時(shí),為什么不能推導(dǎo)出模板參數(shù)?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

這段代碼有什么問(wèn)題?

#include <map>

template<typename T>
struct TMap
{
    typedef std::map<T, T> Type;
};

template<typename T>
T test(typename TMap <T>::Type &tmap_) { return 0.0; }

int _tmain(int argc, _TCHAR* argv[])
{
    TMap<double>::Type tmap;
    tmap[1.1] = 5.2;
    double d = test(tmap); //Error: could not deduce template argument for T
    return 0;
}

推薦答案

那是不可推論的上下文.這就是編譯器無(wú)法推導(dǎo)出模板參數(shù)的原因.

That is non-deducible context. That is why the template argument cannot be deduced by the compiler.

試想一下,如果你有專門(mén)的 TMap 如下:

Just imagine if you might have specialized TMap as follows:

template <>
struct TMap<SomeType>
{
    typedef std::map <double, double> Type;
};

如果 TMap::Typestd::map,編譯器將如何推斷類型 SomeType代碼>?這不可以.不能保證您在std::map中使用的type也是type<TMap 中的/em>.編譯器不能做出這種危險(xiǎn)的假設(shè).無(wú)論如何,type 參數(shù)之間可能沒(méi)有任何關(guān)系.

How would the compiler deduce the type SomeType, given that TMap<SomeType>::Type is std::map<double, double>? It cannot. It's not guaranteed that the type which you use in std::map is also the type in TMap. The compiler cannot make this dangerous assumption. There may not any relation between the type arguments, whatsoever.

此外,您可能還定義了 TMap 的另一個(gè)特化:

Also, you might have another specialization of TMap defined as:

template <>
struct TMap<OtherType>
{
    typedef std::map <double, double> Type;
};

這讓情況變得更糟.現(xiàn)在您擁有以下內(nèi)容:

This makes the situation even worse. Now you've the following:

  • TMap::Type = std::map.
  • TMap::Type = std::map.
  • TMap<SomeType>::Type = std::map<double, double>.
  • TMap<OtherType>::Type = std::map<double, double>.

現(xiàn)在問(wèn)問(wèn)自己:給定TMap::Typestd::map,編譯器如何知道TSomeType 還是 OtherType?它甚至不知道有多少個(gè)這樣的選擇,它自己也不知道選擇...

Now ask yourself: given TMap<T>::Type is std::map<double, double>, how would the compiler know whether T is SomeType or OtherType? It cannot even know how many such choices it has, neither can it know the choices themselves...

我只是為了思想實(shí)驗(yàn)而問(wèn)你(假設(shè)它可以知道完整的選擇集).

I'm just asking you for the sake of thought-experiment (assuming it can know the complete set of choices).

這篇關(guān)于當(dāng)模板參數(shù)用作另一個(gè)模板的模板參數(shù)時(shí),為什么不能推導(dǎo)出模板參數(shù)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉(zhuǎn)換為 HSV 并將 HSV 轉(zhuǎn)換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉(zhuǎn)換為字符串?)
When to use inline function and when not to use it?(什么時(shí)候使用內(nèi)聯(lián)函數(shù),什么時(shí)候不使用?)
Examples of good gotos in C or C++(C 或 C++ 中好的 goto 示例)
Significance of ios_base::sync_with_stdio(false); cin.tie(NULL);(ios_base::sync_with_stdio(false) 的意義;cin.tie(NULL);)
Is TCHAR still relevant?(TCHAR 仍然相關(guān)嗎?)
主站蜘蛛池模板: 亚洲国产精品久久 | 91av在线不卡 | 日韩在线播放视频 | 综合激情久久 | 国产一二三区电影 | 成人免费大片黄在线播放 | 日韩欧美国产成人一区二区 | 综合久久99 | 欧美日韩国产精品一区二区 | 九九精品在线 | 亚洲iv一区二区三区 | 国产福利网站 | 久久久久国产 | 免费a网站 | 日韩一级免费观看 | 欧美日韩精品一区二区三区蜜桃 | 亚洲高清免费观看 | 岛国av免费观看 | 欧美无乱码久久久免费午夜一区 | 免费在线观看av | 欧美一区二区三区在线 | 婷婷成人在线 | 亚洲网在线 | 欧美中文视频 | 日韩一区二区三区在线观看 | 久久99精品久久久97夜夜嗨 | 久久久久久亚洲国产精品 | 欧美成人a| 国产精品久久久久久久岛一牛影视 | 中文字幕av一区二区三区 | 少妇午夜一级艳片欧美精品 | 亚洲欧美一区二区三区1000 | 欧美乱码精品一区二区三区 | 欧美极品一区二区 | 精品国产伦一区二区三区观看说明 | 国产97久久| 一区二区三区视频在线 | 美国a级毛片免费视频 | 91日日 | 久久三区 | 国产精品精品视频 |