더 이상 사용되지 않는 것으로 C ++ 표시
휴대용 C ++에서 더 이상 사용하지 않으려는 인터페이스에 메소드가 있습니다. 내가 이것을 구글 검색했을 때 내가 얻은 것은 마이크로 소프트 전용 솔루션이었다. 의 #pragma는 추천 및 __declspec는 (사용되지 않음) .
두 번째 상 솔루션은 MSVC와 GCC 솔루션을 ifdef하는 것입니다.
감사
C ++ 14에서는 [[deprecated]]
속성 을 사용하여 함수가 더 이상 사용되지 않는 것으로 표시 할 수 있습니다 (섹션 7.6.5 [dcl.attr.deprecated] 참조).
속성 토큰은
deprecated
표시 이름과 그 사용을 계속 허용되는 개체에 사용할 수 있지만, 어떤 이유로 좋습니다.
예를 들어 다음 기능 foo
은 더 이상 사용되지 않습니다.
[[deprecated]]
void foo(int);
이름 또는 엔티티가 더 이상 사용되지 않는 이유를 설명하는 메시지를 제공 할 수 있습니다.
[[deprecated("Replaced by bar, which has an improved interface")]]
void foo(int);
메시지는 문자열 리터럴이어야합니다.
자세한 내용은 “C ++ 14에서 더 이상 사용되지 않는 것으로 표시”를 참조하십시오 .
트릭을 수행해야합니다.
#ifdef __GNUC__
#define DEPRECATED(func) func __attribute__ ((deprecated))
#elif defined(_MSC_VER)
#define DEPRECATED(func) __declspec(deprecated) func
#else
#pragma message("WARNING: You need to implement DEPRECATED for this compiler")
#define DEPRECATED(func) func
#endif
...
//don't use me any more
DEPRECATED(void OldFunc(int a, float b));
//use me instead
void NewFunc(int a, double b);
그러나 함수 리턴 유형에 이름에 쉼표가있는 경우 문제점이 발생합니다. 예 std::pair<int, int>
를 들어 , 이는 선행 작업에서 DEPRECATED 매크로에 2 개의 인수를 전달하는 것으로 해석됩니다. 이 경우 반환 유형을 typedef해야합니다.
편집 : 더 단순하지만 호환성이 낮은 버전은 here .
내 2008 답변 의 단순화 된 버전은 다음과 같습니다 .
#if defined(__GNUC__) || defined(__clang__)
#define DEPRECATED __attribute__((deprecated))
#elif defined(_MSC_VER)
#define DEPRECATED __declspec(deprecated)
#else
#pragma message("WARNING: You need to implement DEPRECATED for this compiler")
#define DEPRECATED
#endif
//...
//don't use me any more
DEPRECATED void OldFunc(int a, float b);
//use me instead
void NewFunc(int a, double b);
또한보십시오:
- 에 대한 MSVC 설명서
__declspec(deprecated)
- 에 대한 GCC 설명서
__attribute__((deprecated))
- 에 대한 Clang 설명서
__attribute__((deprecated))
GCC에서는 다음과 같이 더 이상 사용되지 않는 속성으로 함수를 선언 할 수 있습니다.
void myfunc() __attribute__ ((deprecated));
이 함수가 .c 파일에서 사용될 때 컴파일 타임 경고가 트리거됩니다.
You can find more info under "Diagnostic pragmas" at http://gcc.gnu.org/onlinedocs/gcc/Pragmas.html
Here is a more complete answer for 2018.
These days, a lot of tools allow you to not just mark something as deprecated, but also provide a message. This allows you to tell people when something was deprecated, and maybe point them toward a replacement.
There is still a lot of variety in compiler support:
- C++14 supports
[[deprecated]]
/[[deprecated(message)]]
. __attribute__((deprecated))
is supported by GCC 4.0+ and ARM 4.1+__attribute__((deprecated))
and__attribute__((deprecated(message)))
is supported for:- GCC 4.5+
- Several compilers which masquerade as GCC 4.5+ (by setting
__GNUC__
/__GNUC_MINOR__
/__GNUC_PATCHLEVEL__
) - Intel C/C++ Compiler going back to at least 16 (you can't trust
__GNUC__
/__GNUC_MINOR__
, they just set it to whatever version of GCC is installed) - ARM 5.6+
- MSVC supports
__declspec(deprecated)
since 13.10 (Visual Studio 2003) - MSVC supports
__declspec(deprecated(message))
since 14.0 (Visual Studio 2005)
You can also use [[gnu::deprecated]]
in recent versions of clang in C++11, based on __has_cpp_attribute(gnu::deprecated)
.
I have some macros in Hedley to handle all of this automatically which I keep up to date, but the current version (v2) looks like this:
#if defined(__cplusplus) && (__cplusplus >= 201402L)
# define HEDLEY_DEPRECATED(since) [[deprecated("Since " #since)]]
# define HEDLEY_DEPRECATED_FOR(since, replacement) [[deprecated("Since " #since "; use " #replacement)]]
#elif \
HEDLEY_GCC_HAS_EXTENSION(attribute_deprecated_with_message,4,5,0) || \
HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
HEDLEY_ARM_VERSION_CHECK(5,6,0)
# define HEDLEY_DEPRECATED(since) __attribute__((__deprecated__("Since " #since)))
# define HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__("Since " #since "; use " #replacement)))
#elif \
HEDLEY_GCC_HAS_ATTRIBUTE(deprcated,4,0,0) || \
HEDLEY_ARM_VERSION_CHECK(4,1,0)
# define HEDLEY_DEPRECATED(since) __attribute__((__deprecated__))
# define HEDLEY_DEPRECATED_FOR(since, replacement) __attribute__((__deprecated__))
#elif HEDLEY_MSVC_VERSION_CHECK(14,0,0)
# define HEDLEY_DEPRECATED(since) __declspec(deprecated("Since " # since))
# define HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated("Since " #since "; use " #replacement))
#elif HEDLEY_MSVC_VERSION_CHECK(13,10,0)
# define HEDLEY_DEPRECATED(since) _declspec(deprecated)
# define HEDLEY_DEPRECATED_FOR(since, replacement) __declspec(deprecated)
#else
# define HEDLEY_DEPRECATED(since)
# define HEDLEY_DEPRECATED_FOR(since, replacement)
#endif
I'll leave it as an exercise to figure out how to get rid of the *_VERSION_CHECK
and *_HAS_ATTRIBUTE
macros if you don't want to use Hedley (I wrote Hedley largely so I wouldn't have to think about that on a regular basis).
If you use GLib, you can use the G_DEPRECATED
and G_DEPRECATED_FOR
macros. They're not as robust as the ones from Hedley, but if you already use GLib there is nothing to add.
Dealing with portable projects it's almost inevitable that you at some point need a section of preprocessed alternatives for a range of platforms. #ifdef this #ifdef that and so on.
In such a section you could very well conditionally define a way to deprecate symbols. My preference is usually to define a "warning" macro since most toolchains support custom compiler warnings. Then you can go on with a specific warning macro for deprecation etc. For the platforms supporting dedicated deprecation methods you can use that instead of warnings.
For Intel Compiler v19.0, use this as __INTEL_COMPILER
evaluates to 1900
:
# if defined(__INTEL_COMPILER)
# define DEPRECATED [[deprecated]]
# endif
Works for the following language levels:
- C++17 Support (/Qstd=c++17)
- C++14 Support (/Qstd=c++14)
- C++11 Support (/Qstd=c++11)
- C11 Support (/Qstd=c11)
- C99 Support (/Qstd=c99)
The Intel Compiler has what appears a bug in that it does not support the [[deprecated]]
attribute on certain language elements that all other compilers do. For an example, compile v6.0.0 of the (remarkly superb) {fmtlib/fmt} library on GitHub with Intel Compiler v19.0. It will break. Then see the fix in the GitHub commit.
참고URL : https://stackoverflow.com/questions/295120/c-mark-as-deprecated
'IT' 카테고리의 다른 글
SQL DROP TABLE 외래 키 제약 조건 (0) | 2020.06.22 |
---|---|
D 현실에서 프로그래밍 언어? (0) | 2020.06.22 |
다음 모듈은 최적화를 사용하거나 디버그 정보를 사용하지 않고 구축되었습니다. (0) | 2020.06.22 |
Sinatra로 정적 파일 제공 (0) | 2020.06.22 |
목록 교차점을 찾는 방법은 무엇입니까? (0) | 2020.06.22 |