問題描述
counter
是一個 int
阻止此錯誤的最佳方法是什么?當(dāng)我懶惰時,我只是制作了 int long long
(或其他東西),但我相信有更好的方法來解決這個問題.
錯誤信息:
我使用的是 Visual C++ 2010 Express.
在 VC++ 2010 中,std::to_string
的三個重載需要 long long
、>unsigned long long
和 long double
,分別——很明顯 int
不是這些,沒有一種轉(zhuǎn)換比另一種更好(demo),因此無法隱式/明確地進(jìn)行轉(zhuǎn)換.
就真正的 C++11 支持而言,這是 VC++ 2010 標(biāo)準(zhǔn)庫實(shí)現(xiàn)的一個失敗——C++11 標(biāo)準(zhǔn)本身實(shí)際上要求 九個 重載std::to_string
([string.conversions]/7):
如果所有這些重載都存在,你顯然不會有這個問題;然而,VC++ 2010 并不是基于實(shí)際的 C++11 標(biāo)準(zhǔn)(在發(fā)布時還不存在),而是基于 N3000(來自2009),它不調(diào)用對于這些額外的重載.因此,在這里責(zé)怪 VC++ 太苛刻了...
無論如何,對于少數(shù)調(diào)用,使用強(qiáng)制轉(zhuǎn)換自己解決歧義并沒有錯:
或者,如果您的代碼庫中大量使用 std::to_string
,請編寫一些包裝器并改用它們——這樣,就不需要調(diào)用站點(diǎn)轉(zhuǎn)換:
<小時>
以上SFINAE的使用方式無法檢測VC++ 2010是成功還是失??;如果失敗,以下內(nèi)容——使用標(biāo)簽調(diào)度而不是 SFINAE——應(yīng)該是可編譯的(如果可能不太清楚):
counter
is an int
What would be the best way to stop this error? When I was being lazy I just made the int long long
(or something), but I'm sure there is a better way of solving this.
Error message:
I am using Visual C++ 2010 Express.
In VC++ 2010 there are three overloads of std::to_string
that take long long
, unsigned long long
, and long double
, respectively – clearly int
is none of these, and no one conversion is better than another (demo), so the conversion cannot be done implicitly/unambiguously.
In terms of real C++11 support, this is a failing on the part of VC++ 2010's standard library implementation – the C++11 standard itself actually calls for nine overloads of std::to_string
([string.conversions]/7):
Had all of these overloads been present, you obviously wouldn't have this problem; however, VC++ 2010 wasn't based on the actual C++11 standard (which did not yet exist at the time of its release), but rather on N3000 (from 2009), which does not call for these additional overloads. Consequently, it's harsh to blame VC++ too much here...
In any case, for only a handful of calls, there's nothing wrong with using a cast to resolve the ambiguity yourself:
Or, if there's heavy usage of std::to_string
in your codebase, write a few wrappers and use those instead – this way, no call-site casting is needed:
I can't check whether VC++ 2010 succeeds or fails with the above usage of SFINAE; if it fails, the following – using tag dispatch instead of SFINAE – should be compilable (if potentially less clear):
這篇關(guān)于std::to_string - 多個重載函數(shù)的實(shí)例與參數(shù)列表匹配的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!