0

我发现(使用 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();
          ^
4

1 回答 1

1

[[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.

因此,这指出该属性“只是在错误的位置”(有趣的是,弃用警告稍后仍会根据需要打印!)

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

第二个是我们想要的。

于 2018-01-25T20:03:53.487 回答