[[deprecated]] 与 using 不兼容但与 typedef 兼容

[[deprecated]] incompatible with using but compatible with typedef

我发现(使用 g++ 6.3)我可以使用 [[deprecated]] 属性弃用 typedef ([[deprecated]] typedef longname shortname;) 但不能弃用 using 声明 ([[deprecated]] shortname = longname;) .

所以我想知道,这是故意的吗(如果是,为什么?)?或者这是一个错误?

作为 MWE:

#include <vector>

namespace our { 
  using vector = std::vector<float>;
} 

//[[deprecated("use the namespace 'our'")]] using myvector = std::vector<float>;
[[deprecated("use the namespace 'our'")]] typedef std::vector<float> myvector;

int main() { 
  myvector a;
  return a.size();
} 

根据以下警告(预期)中注释的行给出:

g++  -march=native -std=c++11 -m64 -O2 -g -Wextra -Wall -Wshadow -lstdc++ -m64 -g -march=native -flto  main.cpp   -o main
main.cpp: In function ‘int main()’:
main.cpp:11:12: warning: ‘myvector’ is deprecated: use the namespace 'our' [-Wdeprecated-declarations]
   myvector a;
            ^
main.cpp:8:70: note: declared here
 [[deprecated("use the namespace 'our'")]] typedef std::vector<float> myvector;
                                                                      ^~~~~~~~

或以下错误(意外)

g++  -march=native -std=c++11 -m64 -O2 -g -Wextra -Wall -Wshadow -lstdc++ -m64 -g -march=native -flto  main.cpp   -o main
main.cpp:7:43: error: expected unqualified-id before ‘using’
 [[deprecated("use the namespace 'our'")]] using myvector = std::vector<float>;
                                           ^~~~~
main.cpp: In function ‘int main()’:
main.cpp:11:3: error: ‘myvector’ was not declared in this scope
   myvector a;
   ^~~~~~~~
main.cpp:12:10: error: ‘a’ was not declared in this scope
   return a.size();
          ^

[[deprecated]] 语句不能放在 using 语句中的那个位置(虽然我认为放在前面的行会很好)。

其中

//using [[deprecated("use the namespace 'our'")]] myvector = std::vector<float>;
using myvector [[deprecated("use the namespace 'our'")]] = std::vector<float>;
//using myvector = [[deprecated("use the namespace 'our'")]] std::vector<float>;
//[[deprecated("use the namespace 'our'")]] typedef std::vector<float> myvector;

第一个无法编译,在这种情况下 clang 会给出更好的错误消息

clang++ -Weverything -std=c++14 -O2 -g main.cpp   -o main
main.cpp:4:18: warning: alias declarations are incompatible with C++98 [-Wc++98-compat]
  using vector = std::vector<float>;
                 ^
main.cpp:7:11: warning: C++11 attribute syntax is incompatible with C++98 [-Wc++98-compat]
    using [[deprecated("use the namespace 'our'")]] myvector = std::vector<float>;
          ^
main.cpp:7:11: error: an attribute list cannot appear here
    using [[deprecated("use the namespace 'our'")]] myvector = std::vector<float>;
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                                                             [[deprecated("use the namespace 'our'")]]
main.cpp:7:64: warning: alias declarations are incompatible with C++98 [-Wc++98-compat]
    using [[deprecated("use the namespace 'our'")]] myvector = std::vector<float>;
                                                               ^
main.cpp:13:3: warning: 'myvector' is deprecated: use the namespace 'our' [-Wdeprecated-declarations]
  myvector a;
  ^
main.cpp:7:53: note: 'myvector' has been explicitly marked deprecated here
    using [[deprecated("use the namespace 'our'")]] myvector = std::vector<float>;
                                                    ^
main.cpp:14:10: warning: implicit conversion loses integer precision: 'size_type' (aka 'unsigned long') to
      'int' [-Wshorten-64-to-32]
  return a.size();
  ~~~~~~ ^~~~~~~~
5 warnings and 1 error generated.

所以这指出该属性是 "just in the wrong place"(有趣的是,弃用警告稍后仍按需要打印!)

第三个打印 gcc 警告(虽然不是我们想要的)warning: ignoring attributes applied to class type ‘std::vector<float>’ outside of definition [-Wattributes] 或 clang 错误 'deprecated' attribute cannot be applied to types.

第二种就是我们想要的