Search code examples
c++templatesvariadictrailing-return-type

How to separate declaration and definition for template function with trailing return type?


I stumbled upon the same problem as in this question: Can't Separate Function Into Declaration and Definition with Trailing Return & Template, but there are no answers.

Basically, I want to write the following:

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
}

With g++-9 or g++10 I get the following error:

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
      |        ^~~~~~~~~~~~~~~~~~~~

I don't get what I'm doing wrong.

Is there any way to do that?


Solution

  • Replacing sizeof...(funcs) with sizeof...(Basis_function) seems to work with g++, but I cannot explain why...

    (note that your code works with clang++)