简体   繁体   English

如何将模板 function 的声明和定义与尾随返回类型分开?

[英]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.我偶然发现了与这个问题相同的问题: Can'tseparate Function Into Declaration and Definition with Trailing Return & Template ,但没有答案。

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:使用 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
      |        ^~~~~~~~~~~~~~~~~~~~

I don't get what I'm doing wrong.我不明白我做错了什么。

Is there any way to do that?有没有办法做到这一点?

Replacing sizeof...(funcs) with sizeof...(Basis_function) seems to work with g++, but I cannot explain why...用 sizeof...(Basis_function) 替换sizeof...(funcs) sizeof...(Basis_function)似乎适用于 g++,但我无法解释为什么...

(note that your code works with clang++) (请注意,您的代码适用于 clang++)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何分离子模板类的定义和声明 - How to separate definition and declaration of child template class C++ - 模板 class 中模板 function 的单独声明/定义 - C++ - Separate declaration/definition for template function in template class 如何建立使用模板函数的 lambda function 的尾随返回类型? - How to establish trailing return type of lambda function that use template functions? 使用decltype尾随返回类型来专门化功能模板 - Specialize function template with decltype trailing return type 已具有返回类型的函数声明中的模板声明? - Template declaration in function declaration that already has a return type? 模板声明中省略了c ++类型参数。 但是,在成员函数的定义中呢? - c++ type arguments are omitted in template declaration. However, how about in the definition of member function? 如果不输入别名类型的完整声明,则无法从单独文件中的类模板定义访问类型别名 - Cannot access type alias from class template definition in separate file without typing full declaration of alias type 使用带有可变参数模板函数的 decltype 的尾随返回类型 - trailing return type using decltype with a variadic template function &in函数声明返回类型 - & in function declaration return type 使用 enable_if 单独定义和声明模板成员函数,其模板参数还包括一个 constexpr 成员函数 - Separate definition and declaration of template member function using enable_if whose template parameter also includes a constexpr member function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM