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



我偶然发现了与这个问题相同的问题:Can';t用尾随返回将函数分离为声明和定义&模板,但没有答案。

基本上,我想写以下内容:

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, Basis_function... funcs) -> std::array<double, 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, Basis_function... funcs) -> std::array<double, sizeof...(funcs)>
|      ^~~~~~~~~~~~~~~
ALGLIB_wrappers.h:15:8: note: candidate is: ‘template<class Table, class ... Basis_function> std::array<double, sizeof... (funcs)> ALGLIB_wrappers::Linear_least_squares::fit(const Table&, Basis_function ...)’
15 |   auto fit(const Table&, Basis_function... funcs) -> std::array<double, sizeof...(funcs)>;
|        ^~~
ALGLIB_wrappers.h:8:8: note: ‘class ALGLIB_wrappers::Linear_least_squares’ defined here
8 |  class Linear_least_squares
|        ^~~~~~~~~~~~~~~~~~~~

我不明白我做错了什么。

有办法做到这一点吗?

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

(注意,您的代码可以使用clang++(

最新更新