函数别名模板在 GCC 中给出错误,但在 VS2015 中没有
Function alias template gives an error with GCC, but not with VS2015
下一个代码片段展示了我如何简化用户代码,使库稍微复杂一些。换句话说,添加一些语法糖。
Channel const& joinMulticast(NetAdres const &group) const;
/// Auto-construct NetAdres.
/// joinMulticast() can take any combination of args and passes them to NetAdres::NetAdres() constructor. No need to write many overloads
template<typename... Y>
auto joinMulticast(Y&&... y)
-> decltype(joinMulticast(std::declval<Y>()...))
{
return joinMulticast(NetAdres(std::forward<Y>(y)...));
}
VS2015
默默吃,但是GCC无法消化这段代码,出现致命错误:
template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum)
-> decltype(joinMulticast(std::declval<Y>()...))
~~~~~~~~~~~~~~~^~
因为在我的例子中 return 类型总是相同的,简化构造:
template<typename... Y>
Channel const& joinMulticast(Y&&... y)
{
return joinMulticast(NetAdres(std::forward<Y>(y)...));
}
下一个代码片段展示了我如何简化用户代码,使库稍微复杂一些。换句话说,添加一些语法糖。
Channel const& joinMulticast(NetAdres const &group) const;
/// Auto-construct NetAdres.
/// joinMulticast() can take any combination of args and passes them to NetAdres::NetAdres() constructor. No need to write many overloads
template<typename... Y>
auto joinMulticast(Y&&... y)
-> decltype(joinMulticast(std::declval<Y>()...))
{
return joinMulticast(NetAdres(std::forward<Y>(y)...));
}
VS2015
默默吃,但是GCC无法消化这段代码,出现致命错误:
template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum)
-> decltype(joinMulticast(std::declval<Y>()...))
~~~~~~~~~~~~~~~^~
因为在我的例子中 return 类型总是相同的,简化构造:
template<typename... Y>
Channel const& joinMulticast(Y&&... y)
{
return joinMulticast(NetAdres(std::forward<Y>(y)...));
}