8

I want to rename a templated class. To make the transition easier for the users, I'd like to keep the old class for one more version and mark it deprecated with the extensions from GCC / Clang (attribute deprecated). To avoid keeping an exact copy of the deprecated class, the use of template alias would be handy. Unfortunatley it does not seem to work. This is what I tried with Clang 3.3, GCC 4.7, and GCC 4.8:

template <class blabla>
struct NewClassName
{
    // ...
};

template <class blabla> using OldClassName [[deprecated]]
  = NewClassName<blabla>;

Do I miss something or is this just unsupported by the compilers? Is there an other idea to get deprecation warnings without copying the whole class?

usr1234567
  • 21,601
  • 16
  • 108
  • 128
  • 1
    It works well in 4.8 and 4.7 and makes deprecating warning. [Live code](http://coliru.stacked-crooked.com/a/1623d000527959d8) – masoud Nov 05 '13 at 15:04
  • You are right, I was testing it with a slightly more complex example, it works for 4.7 and newer. Unfortunately Clang 3.3 emits an error. – usr1234567 Nov 05 '13 at 15:12
  • 2
    I would open a bug to clang. – n. m. could be an AI Nov 05 '13 at 16:02
  • 1
    @n.m.: Yes, it seems a bug, however it's just an extension and they've not promised it works with `using`. See [here](http://clang.llvm.org/docs/LanguageExtensions.html#messages-on-deprecated-and-unavailable-attributes) – masoud Nov 05 '13 at 18:10

1 Answers1

7

GCC does support deprecating template alias since version 4.9 (since 4.7 with __attribute__(deprecated)). This is a test case comparing typedef and template alias:

template <class T>
struct NewClassName
{
    // ...
};

template <class T> using OldClassNameUsing [[deprecated]]
  = NewClassName<T>;

typedef NewClassName<int> OldClassNameTypedef [[deprecated]];

int main()
{
  OldClassNameUsing<int> objectUsing;
  OldClassNameTypedef objectTypedef;

  return 0;
}

The reason why it did not work for me was that I did not create an object of the OldClassNameUsing but accessed static members like OldClassNameUsing::myFunction(). This does never trigger a deprecation warning unless the function itself is deprecated.

Clang does not yet support deprecating a template alias - tested with version 13. The corresponding feature request is http://llvm.org/bugs/show_bug.cgi?id=17862 https://github.com/llvm/llvm-project/issues/18236.

usr1234567
  • 21,601
  • 16
  • 108
  • 128
  • Thanks, seems to work in Clang 3.4.1+ for me (including 4.0.0) with C++11: https://godbolt.org/z/5CsB4v – Ax3l May 22 '20 at 01:46
  • @Ax3l No, you get the warning for the `typedef`, not for the `using`. The latter is required to keep the template. – usr1234567 Jul 12 '21 at 19:06