클래스의 특정 템플릿 인스턴스화에 대해서만 멤버 함수를 선언 할 수 있습니까?클래스의 특정 템플릿 인스턴스화에만 멤버 함수 선언
// Polynomial<N> is a polynomial of degree N
template<int N>
class Polynomial {
public:
//... various shared methods e.g...
double eval(double x) const;
Polynomial<N-1> derivative() const;
Polynomial<N+1> integralFrom(double x0) const;
// ... various shared operators etc.
double zero() const; // only want Polynomial<1> to support this
// only want Polynomial<2> and Polynomial<1> to support the following
// because the solutions rapidly become too difficult to implement
std::vector<double> zeros() const;
std::vector<double> stationaryPoints() const { return derivative().zeros();}
private:
std::array<double,2> coeffs;
}
나의 현재 해결 방법은 단지 N>2
에 대한 Polynomial<N>::zeros()
에서 예외가 발생하는 것입니다하지만 컴파일시 문제를 감지 좋았을 것이다 : 나는 그것을하고 싶은 이유입니다.
템플릿 전문화가 가능합니까? – Pumkko
C++ 11이 사용 가능하다면'throw' 대신'static_assert (N <= 2, "잘못된 템플릿 인수.")를 사용할 수 있습니다. 컴파일시 바람직하지 않은 기능을 방지합니다. – ilotXXI
어쨌든 전문화가 필요합니다 ('Polynomial'로 인해) –