下一个代码段显示了如何简化用户代码,使库稍微复杂一些。换句话说,添加一些语法糖。
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>()...))
~~~~~~~~~~~~~~~^~
答案 0 :(得分:0)
由于在我的情况下返回类型总是相同的,所以简化构造:
template<typename... Y>
Channel const& joinMulticast(Y&&... y)
{
return joinMulticast(NetAdres(std::forward<Y>(y)...));
}