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

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

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

問題描述

這段代碼有什么問題?

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

推薦答案

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

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>.編譯器不能做出這種危險的假設(shè).無論如何,type 參數(shù)之間可能沒有任何關(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 的另一個特化:

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)在問問自己:給定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...

我只是為了思想實驗而問你(假設(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ù)用作另一個模板的模板參數(shù)時,為什么不能推導(dǎo)出模板參數(shù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(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?(什么時候使用內(nèi)聯(lián)函數(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)嗎?)
主站蜘蛛池模板: 18视频在线观看 | 毛片网站免费 | 在线少妇 | a视频| 中日韩毛片 | 九九视频在线观看 | 日韩欧美国产高清91 | 51成人做爰www免费看网站 | 精品一区二区国产 | 日韩精品欧美 | 五月天婷婷综合 | 国产精品美女久久久久久久久 | a级黄毛片 | 欧美三级欧美一级 | 青青草手机在线视频 | 免费成人黄色 | 成人精品在线观看 | 欧美日韩在线一区二区 | a视频 | 成人激情视频在线观看 | 日本在线网站 | 国产成人一区二区 | 亚洲无人区一线二线三线 | 国产精品国产成人国产三级 | 欧洲一级片 | 日韩黄色一级视频 | 伊人2222 | 人人综合 | 国产美女视频 | 一区二区免费 | 伊人色综合网 | 永久免费看mv网站入口亚洲 | 国产精品久久久久久久久久久久久久久 | 狠狠干欧美 | 人人草人人 | www.久久久久久 | 亚洲一区欧美一区 | 又色又爽又黄gif动态图 | 日日操日日射 | 国产这里只有精品 | 成人手机在线视频 |