나는 다음과 같은 그것에 템플릿 클래스 및 출력 연산자를 작성했습니다 :출력 연산자는
#include <iostream>
namespace N
{
template< typename ...types >
struct X
{
static_assert((sizeof...(types) != 0), "zero length");
X() = default;
X(X const &) = default;
X(X &&) = default;
template< typename ...args >
//explicit // does not matter
X(args &&...) { ; }
int value = 10;
};
template< typename ...types >
std::ostream &
operator << (std::ostream & out, X<types...> const & x)
{
return out << x.value;
}
} // namespace N
int main()
{
using namespace N;
X<float> /*const*/ x; // `const` does not matter
std::cout << x << std::endl;
return 0;
}
그러나 static_assert
이온을 제기 :
main.cpp:9:5: error: static_assert failed "zero length"
static_assert((sizeof...(types) != 0), "zero length");
^ ~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:32:23: note: in instantiation of template class 'N::X<>' requested here
std::cout << x << std::endl;
^
1 error generated.
만약 클래스 템플릿 X
전역에 namespace
에 정의 된 operator <<
오버로드는 모두 동일합니다. 내가 알기로, 그 대답은 using namespace N;
라인과 대체 X<float>
에서 N::X<float>
로 해결됩니다.
어떻게 이런 행동을 설명 할 수 있습니까? 그 원인은 무엇입니까?
는편집 : 나는 해결책을 찾을 수
은 : 클래스의
template< typename first, typename ...rest >
std::ostream &
operator << (std::ostream & out, X< first, rest... > const & x)
{
return out << x.value;
}
분할 typename ..types
이 nessesarily되지 않습니다 : 다음과 같이 오버로드 operator <<
의 템플릿 매개 변수를 CHAGE하는 것입니다. 게다가 극단적 인 코드의 팽창으로 인해 결과가 전혀 바람직하지 않습니다.
중복 된'std :: endl' 대신' '\ n''을 사용하면 역시 수정됩니다. – Cubbi