大佬教程收集整理的这篇文章主要介绍了c – 解决编译器错误,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
减少版本重现:
namespace cg { template<class S> S epsilon(); template<> double epsilon<double>() {return 1;} template<> float epsilon<float>() {return 1;} template<class S> bool eq(S a,S b,S eps = cg::epsilon<S>()) { return true; } } int main(int argc,char* argv[]) { cg::eq(0.0,0.0); cg::eq(0.0f,0.0f); return 0; }
是否有一些变通方法可以使用访问器?
PS:我们使用cg :: scalar_Traits< S> :: epsilon(),这有助于发生错误,但是太多冗长
研究:
甚至声明为
template<class S> bool eq(S a,S eps = cg::epsilon<double>())
编译器抱怨他无法推断出cg :: epsilon的s.
解决方法是避免第三个参数的默认值并添加两个不同的重载:第一个采用三个参数(a,b和eps),第二个采用两个参数(a和b).后者从cg :: epsilon< S>()得到eps(此时已经推断出S)并将调用委托给前者,如下面的代码所示:
template<class S> bool eq(S a,S eps) { return true; } template<class S> bool eq(S a,S b) { S eps = cg::epsilon<S>(); return eq(a,b,eps); }
以上是大佬教程为你收集整理的c – 解决编译器错误全部内容,希望文章能够帮你解决c – 解决编译器错误所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。