如何将带有尾随返回类型的模板函数的声明和定义分开?

问题描述

我偶然发现了与此问题相同的问题:Can't Separate Function Into Declaration and Definition with Trailing Return & Template,但没有答案。

基本上,我想写以下内容

namespace ALGLIB_wrappers
{
    class Linear_least_squares
    {
        public:
            template<typename Table,typename... Basis_function>
            auto fit(const Table&,Basis_function... funcs) -> std::array<double,sizeof...(funcs)>;
    };
}
template<typename Table,typename... Basis_function>
auto ALGLIB_wrappers::Linear_least_squares::fit(const Table& potential,sizeof...(funcs)>
{
    // code
}

使用 g++-9 或 g++10 时出现以下错误

ALGLIB_wrappers.h:151:6: error: no declaration matches ‘std::array<double,sizeof... (funcs)> ALGLIB_wrappers::Linear_least_squares::fit(const Table&,Basis_function ...)’
  151 | auto ALGLIB_wrappers::Linear_least_squares::fit(const Table& potential,sizeof...(funcs)>
      |      ^~~~~~~~~~~~~~~
ALGLIB_wrappers.h:15:8: note: candidate is: ‘template<class Table,class ... Basis_function> std::array<double,Basis_function ...)’
   15 |   auto fit(const Table&,sizeof...(funcs)>;
      |        ^~~
ALGLIB_wrappers.h:8:8: note: ‘class ALGLIB_wrappers::Linear_least_squares’ defined here
    8 |  class Linear_least_squares
      |        ^~~~~~~~~~~~~~~~~~~~

我不明白我做错了什么。

有没有办法做到这一点?

解决方法

sizeof...(funcs) 替换 sizeof...(Basis_function) 似乎适用于 g++,但我无法解释为什么...

(请注意,您的代码适用于 clang++)