2017-11-12 16 views
3

현재 첫 번째 개념을 작성 중입니다. 컴파일러는 -fconcepts를 사용하여 g ++ 7.2를 호출합니다. 내 개념은 다음과 같습니다.개념을 멤버 변수에 적용하는 방법

template <typename stack_t> 
concept bool Stack() { 
    return requires(stack_t p_stack, size_t p_i) { 
     { p_stack[p_i] }; 
    }; 
}; 

template <typename environment_t> 
concept bool Environment() { 
    return requires(environment_t p_env) { 
     { p_env.stack } 
    }; 
}; 

위와 같이 환경에 stack이라는 멤버가 있어야합니다. 이 멤버는 Stack이라는 개념과 일치해야합니다. 환경에 이러한 요구 사항을 어떻게 추가합니까?

답변

1

이 솔루션을 gcc 6.3.0 및 -fconcepts 옵션으로 테스트했습니다.

#include <iostream> 
#include <vector> 

template <typename stack_t> 
concept bool Stack() { 
    return requires(stack_t p_stack, size_t p_i) { 
     { p_stack[p_i] }; 
    }; 
}; 

template <typename environment_t> 
concept bool Environment() { 
    return requires(environment_t p_env) { 
     { p_env.stack } -> Stack; //here 
    }; 
}; 

struct GoodType 
{ 
    std::vector<int> stack; 
}; 

struct BadType 
{ 
    int stack; 
}; 

template<Environment E> 
void test(E){} 

int main() 
{ 
    GoodType a; 
    test(a); //compiles fine 

    BadType b; 
    test(b); //comment this line, otherwise build fails due to constraints not satisfied 

    return 0; 
}