클래스에 push
의 두 가지 구현이 있고 부울 템플릿 인수를 기반으로 선택하겠습니다. 과 같이, this answer에 설명 된대로 나는 SFINAE 원리를 사용하여 시도 :부울 템플릿 매개 변수를 사용하여 멤버 함수를 사용하려면 어떻게해야하나요?
template<class T, bool foo=true>
class Bar {
template <>
typename std::enable_if<foo>::type
push(const T& value) { /* one implementation */}
template <>
typename std::enable_if<!foo>::type
push(const T& value) { /* another implementation */ }
}
그러나, 나는 GCC에서 "클래스의 범위 내에서 함수 push
을 전문으로 수 없습니다"의 오류를 얻고, 난 왜 이해가 안 돼요. 내 코드가 링크 된 답변의 코드와 정확히 같지는 않지만 매우 유사하며 중요한 차이점을 발견 할 수 없습니다.
template <bool enable=foo>
typename std::enable_if<enable>::type
push(const T& value) { /* one implementation */}
template <bool enable=!foo>
typename std::enable_if<enable>::type
push(const T& value) { /* another implementation */ }
가 어떻게 이러한 목표를 달성 할 수 있습니까?
와우, 그건 완전히 작동합니다. 고맙습니다. –