문제

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)::cycledecltype(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 유형에 명시 적으로 액세스하는 경우에만.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top