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

當模板參數用作另一個模板的模板參數時,為什

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

問題描述

這段代碼有什么問題?

#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;
}

推薦答案

那是不可推論的上下文.這就是編譯器無法推導出模板參數的原因.

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

試想一下,如果你有專門的 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>.編譯器不能做出這種危險的假設.無論如何,type 參數之間可能沒有任何關系.

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 的另一個特化:

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

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

這讓情況變得更糟.現在您擁有以下內容:

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>.

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

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...

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

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

這篇關于當模板參數用作另一個模板的模板參數時,為什么不能推導出模板參數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both(將 RGB 轉換為 HSV 并將 HSV 轉換為 RGB 的算法,范圍為 0-255)
How to convert an enum type variable to a string?(如何將枚舉類型變量轉換為字符串?)
When to use inline function and when not to use it?(什么時候使用內聯函數,什么時候不使用?)
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 仍然相關嗎?)
主站蜘蛛池模板: 国产视频a| 欧美在线观看视频 | 久久精选视频 | 久久精品国产亚洲 | 欧美成人精品欧美一级私黄 | 色综合久 | 国产午夜影院 | 色香蕉视频 | www.国产.com | 日韩欧美国产精品 | 黄色录像免费看 | 精品欧美一区二区精品久久 | 色婷婷精品国产一区二区三区 | 欧美黄色片在线观看 | 国产三级成人 | 在线天堂视频 | av毛片网站 | 四级黄色片 | 日韩高清精品免费观看 | 亚洲精品久久久久久久久久久 | 人人草人人 | 男人午夜影院 | 亚洲天堂av网 | 97精品在线| 日韩色在线| 99久久婷婷国产综合精品草原 | 日韩综合一区 | 日本精品视频在线 | 国产视频在线观看视频 | 深夜福利av| 视频一二区 | 亚洲最大黄色网址 | 国产专区在线播放 | 五十路av | 国产一区在线播放 | 99色在线 | 欧美性爽 | 日本少妇做爰全过程毛片 | 国产精品第二页 | 国产精品久久久久久无人区 | 免费午夜视频 |