Deprecate templated class name with template alias (type alias, using)?
Asked Answered
C

1

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?

Churrigueresque answered 5/11, 2013 at 14:56 Comment(4)
It works well in 4.8 and 4.7 and makes deprecating warning. Live codeMccutchen
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.Churrigueresque
I would open a bug to clang.Hie
@n.m.: Yes, it seems a bug, however it's just an extension and they've not promised it works with using. See hereMccutchen
C
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.

Churrigueresque answered 12/11, 2013 at 6:44 Comment(2)
Thanks, seems to work in Clang 3.4.1+ for me (including 4.0.0) with C++11: godbolt.org/z/5CsB4vRabah
@Rabah No, you get the warning for the typedef, not for the using. The latter is required to keep the template.Churrigueresque

© 2022 - 2024 — McMap. All rights reserved.