[英]Template specialization only in class scope
I have a class templatized on a parameter N, in it's own header file reproduction.H:我有一个 class 在参数 N 上模板化,在它自己的 header 文件复制中。H:
#include <cstdlib>
#include <array>
template<std::size_t N>
class A{
private:
std::array<float, N> foo();
};
I'm trying to provide different specializations for the function depending on N.我正在尝试根据 N 为 function 提供不同的专业化。
In the.C file, I have:在 .C 文件中,我有:
#include "reproduction.H"
#include <tuple>
template <std::size_t N>
std::array<float,N> A<2>::foo(){return std::array<float, N>();}
template class A<2>;
And gcc 6 with c++14 gives me error gcc 6 和 c++14 给我错误
reproduction.C:5:21: error: prototype for 'std::array<float, N> A<2ul>::foo()' does not match any in class 'A<2ul>'
std::array<float,N> A<2>::foo(){return std::array<float, N>();}
^~~~
In file included from reproduction.C:1:0:
reproduction.H:6:24: error: candidate is: std::array<float, N> A<N>::foo() [with long unsigned int N = 2ul]
std::array<float, N> foo();
But, if I drop the N, and just have但是,如果我放弃 N,然后
template <>
std::array<float,2> A<2>::foo(){return std::array<float, 2>();}
That compiles, and编译,并且
template <std::size_t N>
std::array<float,N> A<N>::foo(){return std::array<float, N>();}
compiles.编译。
Why would the first case fail, but the next two pass?为什么第一个案例会失败,但接下来的两个案例会通过?
This这个
template <std::size_t N>
std::array<float,N> A<N>::foo(){return std::array<float, N>();}
is definition of a member function of a primary template.是主模板的成员 function 的定义。
This这个
template <>
std::array<float,2> A<2>::foo(){return std::array<float, 2>();}
is explicit specialization of the same member function.是同一成员 function 的显式特化。
This这个
template <std::size_t N>
std::array<float,N> A<2>::foo(){return std::array<float, N>();}
is invalid because the template argument list does not correspond to the template parameter list.无效,因为模板参数列表与模板参数列表不对应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.