久久久久久久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)嗎?)
主站蜘蛛池模板: 亚洲热在线视频 | av片免费| 亚洲天堂成人在线视频 | 一本色道精品久久一区二区三区 | 色综网 | 中文字幕免费在线 | 国内精品久久久久 | 精品日韩一区 | 在线一区 | 精品在线| 国产麻豆一区二区三区 | 中文字幕精品一区二区三区精品 | av天天澡天天爽天天av | 成人国产精品免费观看 | 免费观看av网站 | 精品欧美一区二区三区久久久 | 亚洲高清在线免费观看 | 欧美极品在线 | 久久亚洲精品国产精品紫薇 | 青草视频在线 | 亚洲视频在线观看免费 | 91一区二区三区 | av网址在线播放 | av免费网址 | 久久久免费精品 | 国产精品美女一区二区三区 | 午夜影院在线观看 | 天天射天天干 | 国产www成人 | 久久精品高清视频 | 亚洲高清在线观看 | 欧美大片一区 | 成人婷婷 | 亚洲一区在线日韩在线深爱 | 人妖videosex高潮另类 | 日韩精品久久久久 | 欧美激情精品久久久久久变态 | 99视频在线播放 | 麻豆久久久久久久久久 | 综合久久久久久久 | 懂色tv |