考慮以下系統:禁用默認模板,並且只使用專門通過SFINAE
template<typename T>
struct wrapper
{
operator T *() { return nullptr; }
};
template<typename Ret, typename T>
Ret func(T);
template<>
int func(float * in)
{
std::cout << "long";
}
template<>
long func(float * in)
{
std::cout << "int";
}
包裝的目的是允許它衰減到它的模板的類型(它是圍繞一個緩衝包裝類型)。此外,我有一套功能模板的模板專業化。這是爲了避免僅基於返回類型重載時的常見錯誤。
這不工作,雖然,如前所述這裏:相反
// the following should work, but doesn't because it's instantiating
// the func<ret, wrapper<float>> which doesn't exist resulting in a linker error
// instead of selecting the int func(float *) overload
wrapper<float> w;
func<int>(w);
,我想這生成編譯時錯誤(但同樣,它生成一個鏈接時錯誤):
// the following should generate a compile-time error
// since no explicit overload for int func(int *) exists
wrapper<int> w2;
func<int>(w2);
所以理想情況下,我想禁用原始模板(如果可能的話可以通過sfinae?),這樣重載決議只考慮明確的特化,並且如果沒有找到匹配就會產生編譯時錯誤。這可以做到嗎?
鏗鏘和msvc之間的便攜式解決方案是必須的,但我使用兩者的最新版本。
您的問題,還不如說是模板參數推導,不考慮隱式轉換儘可能多的重載決議。 – 2014-10-06 16:04:02
@ P0W因爲你不能超載這兩個。唯一的區別是返回類型。 – 2014-10-06 16:14:17
@TC Ahh我看到了,謝謝 – P0W 2014-10-06 16:15:25