函数别名模板给出了GCC错误,但VS2015没有

时间:2017-01-07 23:46:41

标签: c++11 gcc visual-c++ c++14 syntactic-sugar

下一个代码段显示了如何简化用户代码,使库稍微复杂一些。换句话说,添加一些语法糖。

 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>()...))
                                   ~~~~~~~~~~~~~~~^~

1 个答案:

答案 0 :(得分:0)

由于在我的情况下返回类型总是相同的,所以简化构造:

template<typename... Y>
Channel const& joinMulticast(Y&&... y)
{
  return joinMulticast(NetAdres(std::forward<Y>(y)...));
}