简体   繁体   中英

Can a concept be declared deprecated?

I just encountered a case where I wanted to declare a C++ 20 concept deprecated. However it seems like my compiler (Apple Clang 14) does not accept adding a [[deprecated]] attribute to a concept . My attempt looked like

template <class T>
concept [[deprecated ("Some explanation")]] myConcept = someBoolCondition<T>;

Is deprecating concepts simply not supported (yet), did I choose a wrong syntax or is this a flaw of my compiler?

The possibility to add [[deprecated]] to a concept definition has been added only recently as a defect report with resolution of CWG 2428 .

However, the attribute belongs after the concept's name, not before it:

template <class T>
concept myConcept [[deprecated ("Some explanation")]] = someBoolCondition<T>;

Your compiler is older than the resolution, so it can't have implemented it yet. It seems Clang hasn't implemented it yet at all, but future versions probably will.

GCC trunk does implement the DR, so the next GCC release (version 13), probably will as well.

Latest MSVC does not seem to implement it yet.

I haven't found a way to make make the attribute work directly on the concept but you can add it to the implementation:

template<class T>
struct someBoolCondition_impl {
    // needed for clang/msvc:
    [[deprecated ("Some explanation")]] static constexpr bool value = true;
};

template<class T>
// needed for gcc:
[[deprecated ("Some explanation")]] 
inline constexpr bool someBoolCondition = someBoolCondition_impl<T>::value;

template <class T>
concept myConcept = someBoolCondition<T>;

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM