2014-12-15 3 views
8

나는이 할 싶습니다noexcept 함수 포인터의 별칭은 어떻게 만듭니 까?

using function_type = void (*)(void*)noexcept; 

을하지만 오류 얻을 "예외 사양이 유형의 별칭에서 허용되지 않습니다." (Xcode 버전 6.1에 포함)

noexcept 지정자로 별칭을 만드는 해결 방법이 있습니까?

플랫폼 간 기능을위한 언어 (확장명 아님)로 정의 된대로 작동하는 것을 찾고 있습니다.

+1

g ++가 잘 컴파일됩니다. 어쩌면 지정자를 무시했을 수도 있습니다 ... – DarioP

+1

GCC는 여기에서 표준을 따르지 않습니다. Clang이 맞습니다. – Columbo

+1

흠, 그것은 이상한 gcc 버그입니다. Ut는 typedef를 허용하지 않고'using '을 허용합니다. –

답변

9

표준은 예외 지정이 typedef 또는 별칭 선언에 나타나지 않도록 명시 적으로 금지합니다. 그러나 예외 지정자는 함수 포인터 유형에 나타날 수도 있습니다.

§15.4/2[except.spec]

An exception-specification shall appear only on a function declarator for a function type, pointer to function type, reference to function type, or pointer to member function type that is the top-level type of a declaration or definition, or on such a type appearing as a parameter or return type in a function declarator. An exception-specification shall not appear in a typedef declaration or alias-declaration.

및 함수 포인터 예외 지정이없는 경우, 그 함수 포인터는 항상를 갖는 기능 타입을 할당해야 호환 예외 사양. 이 두 가지를 사용

§15.4/5

... A similar restriction applies to assignment to and initialization of pointers to functions, pointers to member functions, and references to functions: the target entity shall allow at least the exceptions allowed by the source value in the assignment or initialization. ...

, 당신은 로터리 방식으로 함수 포인터 형식으로 noexcept 사양을 얻을 수 있습니다.

void (*foo_ptr)(void *) noexcept = nullptr; 
using function_type = decltype(foo_ptr); 

지금, 당신은 유형 function_type의 함수 포인터에 noexcept(true) 사양하지 않고 기능을 할당 할 수 없습니다. clang will fail to compile 오류

error: target exception specification is not superset of source

+1

이 제한에 대한 이론적 근거는 무엇입니까? – kec

+0

§15.4/4가 할당이나 초기화를 다루지 않는다고 생각합니다. 당신은 아마도 §15.4/5의 두 번째 단락을 원할 것입니다. –

+0

@kec 제 생각에 함수 유형의 일부로 예외 스펙을 사용하지 않는 것이 좋습니다. – Praetorian

3

변수를 선언 포함되지 않습니다 근위병의 대답에 대한 대안과 코드 :

void unused_function(void*)noexcept; 
using function_type = decltype(&unused_function); 

unused_function 선언하지만, 정의되어 있지 않습니다.