재귀 적으로 정의 된 중첩 유형 (불완전한 유형의 측면에서)
-
20-12-2019 - |
문제
cycle
Break의 정의에서 재귀가 어디에서 수행됩니까?
#include <iostream>
using namespace std;
template<typename T>
struct Recursive
{
using cycle = struct X : Recursive<X> {}; // would work for Recursive<T> as well
};
int main()
{
Recursive<int> x;
return 0;
}
.
위의 코드 컴파일 -는그것은 유효한 코드와 그렇다면 Type Type cycle
의 의미 (간단한 설명)입니다.
해결책
struct X : Recursive<X>
는 호기심이 가장 반복되는 템플릿 패턴의 예이지만 중첩 된 cycle
유형에 액세스하지 않는 한 no 무한 재귀 이 없습니다.예를 들어,decltype(x)::cycle
는 decltype(x)::cycle::cycle
와 다른 유형입니다.
#include <iostream>
#include <type_traits>
#include <typeinfo>
#include <cxxabi.h>
using namespace std;
template<typename T>
struct Recursive
{
using cycle = struct X : Recursive<X> {};
};
int main()
{
int status;
Recursive<int> x;
std::cout << abi::__cxa_demangle(typeid(x).name(), 0, 0, &status) << '\n';
std::cout << abi::__cxa_demangle(typeid(decltype(x)::cycle).name(), 0, 0, &status) << '\n';
std::cout << abi::__cxa_demangle(typeid(decltype(x)::cycle::cycle).name(), 0, 0, &status) << '\n';
return 0;
}
.
인쇄
Recursive<int>
Recursive<int>::X
Recursive<Recursive<int>::X>::X
재생이 영원히 계속 될 것입니다. 그러나 추가 중첩 된 cycle
유형에 명시 적으로 액세스하는 경우에만.
제휴하지 않습니다 StackOverflow