이 질문은 a previous Q&A에 연결되어 있으며 gcc에 대한 버그 보고서 (gcc 4.5.0에서 수정 된 것으로 추정 됨)와 중첩 클래스 템플릿의 부분 특수화에 대한 일부 특성이 관련되어 있습니다.상속 된 중첩 클래스 템플릿의 부분 특수화 이해
내 설치는 (명시 적 speciliaztion은 동급 허용되지 않기 때문에, 더미 변수 트릭을 사용하여) 내가 char
부분적를 전문으로하는 중첩 된 클래스 템플릿 Inner
와 클래스 Base
을 가지고있다.
#include <type_traits>
#include <iostream>
#include <ios>
struct Base
{
// dummy template parameter...
template<class U, class _ = void> struct Inner: std::true_type {};
// ... to allow in-class partial specialization
template<class _> struct Inner<char, _>: std::false_type {};
};
지금은 내가 더 어떤 이상한 이유로 (가 여전히 부분 특수화 경우에도) 동급 할 수없는 Inner
를 전문으로 할에 대한 Derived
클래스를 정의합니다.
struct Derived
:
Base
{
// cannot partially specialize Inner inside Derived...
//template<class _>
//struct Inner<int, _>: std::false_type {};
};
// ... but specializing Derived::Inner at namespace scope, also specializes it for Base::Inner
template<class _> struct Derived::Inner<int, _>: std::false_type {};
첫 번째 질문 : 왜 부분적으로 네임 스페이스 범위에서 Derived::Inner
을 전문으로해야합니까?
는하지만 이상한 부분은 내가 모두 Base
및 Derived
, 난 단지 Derived
위해 한 int
에 대한 부분 특수화에서 Inner
의 다양한 부분 특수화를 호출 할 때 또한 Base
에 적용됩니다.
int main()
{
std::cout << std::boolalpha << Base::Inner<float>::value << "\n";
std::cout << std::boolalpha << Derived::Inner<float>::value << "\n";
std::cout << std::boolalpha << Base::Inner<char>::value << "\n";
std::cout << std::boolalpha << Derived::Inner<char>::value << "\n";
std::cout << std::boolalpha << Base::Inner<int>::value << "\n"; // huh???
std::cout << std::boolalpha << Derived::Inner<int>::value << "\n"; // OK
}
두 번째 질문 : 왜에만 Derived::Inner<int>
일부 특수에도 불구하고, false
동일 Base::Inner<int>::value
입니까?
Online example using gcc 4.8.0. 나는이 행동을 설명하는 표준에서 인용문을 찾고있다.
tnx for the answer + standard quote! – TemplateRex
내 기쁨! 클래스 외부에서'Derived :: Inner'의 선언이 합법적인지 또는 컴파일러 버그인지를 여전히 허용하는지 잘 모르겠습니다. – willj