2016-10-30 2 views
9

개념 TS에서 제안한대로 typedef 또는 using을 사용하여 개념 안에 형식 별칭을 선언 할 수 있습니까? 개념의 TS에 따르면C++ 개념 lite 및 형식 별칭 선언

main.cpp: In function ‘concept bool TestConcept()’: 
main.cpp:8:9: error: expected primary-expression before ‘using’ 
     using V = T; 
     ^~~~~ 
main.cpp:8:9: error: expected ‘}’ before ‘using’ 
main.cpp:8:9: error: expected ‘;’ before ‘using’ 
main.cpp:4:14: error: definition of concept ‘concept bool TestConcept()’ has multiple statements 
concept bool TestConcept() 
       ^~~~~~~~~~~ 
main.cpp: At global scope: 
main.cpp:11:1: error: expected declaration before ‘}’ token 
} 
^ 
+0

'typedef V T;'를 사용하면 'T'에서 'V'로 별칭을 지정하는 것처럼 보입니다. 'using'은 네임 스페이스 또는 네임 스페이스의 특정 식별자를 호출하는 데 사용됩니다. 여기 예가 있습니다 : http://stackoverflow.com/questions/10103453/is-typedef-inside-of-a-function-body-a-bad-programming-practice –

+1

@JamesMurphy 죄송하지만, C++ 11부터 'typedef'를 사용하여 이전처럼했던'alias '를 표현하기 위해'using' 키워드를 사용하십시오. 다음은 참조입니다 : http://en.cppreference.com/w/cpp/language/type_alias. – erikzenker

+0

@JamesMurphy 예제는 기본적으로 같은 오류 메시지와 함께 typedef와 함께 실패합니다. erikzenker가 말했듯이, 구문은 요즘과 동일해야합니다. – Slizzered

답변

2

번호 : 나는 다음과 같은 MWE 뭔가를하려고하면 이 코드는

#include <type_traits> 

template<typename T> 
concept bool TestConcept() 
{ 
    return requires(T t) 
    { 
     using V = T; 
     std::is_integral<V>::value; 
    }; 
} 

int main() 
{ 
    return 0; 
} 

(GCC 6.2.1과 -fconcepts 스위치) 결과 오류를 컴파일되지 않습니다 , 요구 사항은 다음과 같습니다

requirement:
    simple-requirement
    type-requirement
    compound-requirement
    nested-requirement

간단한 요구 사항표현 인 경우 이온 다음에 ;유형 요구 사항typename T::inner과 같습니다. 다른 두 개는 이름에서 알 수 있듯이 들립니다.

형식 별칭은식이 아닌 선언이므로 요구 사항의 요구 사항을 충족하지 못합니다.

+0

이것은 불필요하게 나를 제한적으로 느낀다. 동일한 복잡한 유형을 반복해서 쓰는 대신 합리적인 해결 방법이 있는지 알고 있습니까? – Slizzered

2

This feels unnecessarily restrictive to me. Do you know if there exists a reasonable workaround instead of writing the same complicated type over and over again?

당신은 템플릿 매개 변수로 그 유형을 전달하는 또 다른 개념에 대한 제약 조건의 이행을 연기 할 수 있습니다

template<typename Cont, typename It, typename Value> 
concept bool InsertableWith = requires(Cont cont, It it, Value value) { 
    // use It and Value as much as necessary 
    cont.insert(it, std::move(value)); 
}; 

template<typename Cont> 
concept bool Insertable = requires { 
    // optional 
    typename Cont::const_iterator; 
    typename Cont::value_type; 
} && InsertableWith<Cont, typename Cont::const_iterator, typename Cont::value_type>; 

당신이 그 일을 고려하는 경우에, 나는 당신이하기 전에 간단한 예제에 그것을 시도 제안 결정. 개념과 제약 조건을 작성하는 방법은 컴파일러가 오류를보고하는 방법을 결정하며 물론 오류가 발생하는 것은 개념을 유용하게 만드는 요소의 큰 부분입니다. 오류를 쉽게 이해할 수 있도록하면서 개념을 쉽게 작성하는 것은 내가 가볍게 받아 들일 절충안이 아닙니다.

예를 들어, 내가 명시 적 제한으로 typename Cont::const_iterator;을 중복 적으로 추가 한 이유입니다. 이렇게하면 컴파일러는이 유형 요구 사항을보고 할 수 있습니다. 또한 개념 이름으로 InsertableWith을 선택하는 데주의를 기울였습니다. detail::Insertable으로 쉽게 이동할 수 있었지만 Insertabledetail::Insertable과 관련된 오류가 더 혼란 스러울 수있었습니다.

마지막으로이 모든 것은 컴파일러의 구현 품질에 따라 달라 지므로 당분간은 어떤 접근 방식도 확실하지 않다고 생각합니다. 이 Coliru demo을 가지고 노는 것이 좋습니다.