[英]ADL related GCC 4.7.2 issue with expression SFINAE
采用以下代碼,其特點是
volume
) namespace Nature {
struct Plant {};
double volume(Plant){ return 3.14; }
}
namespace Industrial {
struct Plant {};
double volume(Plant) { return 100; }
}
namespace SoundEffects {
// A workaround for GCC, but why?
////template<class T> void volume();
template<class aSound>
auto mix(aSound& s) -> decltype(volume(s)*0.1)
{
return volume(s)*.1;
}
struct Samples {
Nature::Plant np;
Industrial::Plant ip;
};
inline double mix(const Samples& s) {
return mix(s.np) + mix(s.ip);
}
}
int main()
{
SoundEffects::Samples s;
assert( mix(s) == 100*.1 + 3.14*.1 );
}
顯示的代碼(沒有template<class T> void volume()
行),VS 2012和clang 3.5成功編譯,運行時是預期的。 但是,GCC 4.7.2說:
template-function-overload.cpp: In substitution of 'template<class aSound> decltype ((volume(s) * 1.0000000000000001e-1)) SoundEffects::mix(aSound&) [with aSound = SoundEffects::Samples]':
template-function-overload.cpp:46:4: required from here
template-function-overload.cpp:23:9: error: 'volume' was not declared in this scope
template-function-overload.cpp:23:9: note: suggested alternatives:
template-function-overload.cpp:9:11: note: 'Nature::volume'
template-function-overload.cpp:14:11: note: 'Industrial::volume'
使用額外的template volume
行,所有三個編譯並運行正常。
所以,這里顯然存在編譯器缺陷。 我的問題是,哪個編譯器是有缺陷的? 哪個C ++標准被違反了?
這是自GCC 4.8以來修復的錯誤。 這是代碼的簡化版本 ,它提供了相同的錯誤:
template<class T>
auto buzz(T x) -> decltype(foo(x));
void buzz(int);
int main() {
buzz(5); // error: 'foo' was not declared in this scope
}
在mix(s)
, SoundEffects::mix
兩個重載都通過ADL編譯成候選重載集( SoundEffects
是SoundEffects::Sample
的關聯命名空間)。 評估函數模板重載的可行性。 錯誤發生,因為volume(s)
不能被解析為一個合適的過載或者通過純不合格查找或ADL為Sample
。
這應該通過的原因是當查找失敗時,應該發生替換失敗(因為volume(s)
是依賴的),並且應該從重載解析中拒絕模板。 這將使mix(const Sample&)
成為唯一可行的重載選擇。 存在硬錯誤的事實顯然表明該GCC版本具有錯誤的SFINAE實現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.