c++ - 解决编译器错误

标签 c++ visual-studio templates compiler-bug

我有大型计算几何库。它的内核有问题。我们有定义标量 taits 和自由函数形式的辅助访问器,可以简单地编写 cg::epsilon<T>()而不是 cg::scalar_traits<T>::epsilon .但问题是在 vs2008 和 vs2010 下它有时会争辩说它无法推断出 T 的模板参数。在 cg::epsilon<T> .在 LWS 中的其他编译器上工作正常。

要重现的缩减版本:

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 b, S eps = cg::epsilon<double>())

编译器提示说他无法为 cg::epsilon 推导出 S。

最佳答案

我的猜测是编译器在推导 S eps = cg::epsilon<S>() 时使用了默认参数 S 。为此,它需要查看 cg::epsilon<S>() 的声明,但此时它还不知道 S

解决方法是避免第三个参数的默认值并添加两个不同的重载:第一个采用三个参数( abeps ),第二个只采用两个( ab )。后者从eps得到cg::epsilon<S>()(此时S已经推导出来)并将调用委托(delegate)给前者,如下代码所示:

template<class S>
bool eq(S a, S b, S eps)
{
    return true;
}

template<class S>
bool eq(S a, S b)
{
    S eps = cg::epsilon<S>();
    return eq(a, b, eps);
}

关于c++ - 解决编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15520497/

相关文章:

C++ cin >> double 不科学

c# - 如何在 Visual Studio 中使用空格自动对齐相似的代码行

c++ - 如何为 C++ 模板编写测试?

c++ - 编写 OpenMAX IL 组件,从哪里开始

c++ - 一般来说,boost bind 在幕后是如何工作的?

c++ - Raspberry Pi 上的浮点性能(ARM 架构,BCM2835)

c++ - 未解析的外部符号

visual-studio - Visual Studio 2017 HRESULT : 0x80004004

c++ - C++中忽略的特征特化

c++ - 如果一个类型 `T` 有一个 `template<> struct Writer<T>` 来序列化自己