[[deprecated]]与使用不兼容但与typedef兼容

时间:2018-01-25 14:59:09

标签: g++ c++14 typedef deprecation-warning using-declaration

我发现(使用g ++ 6.3)我可以使用[[deprecated]]属性来弃用typedef([[deprecated]] typedef longname shortname;)而不是using声明([[deprecated]] shortname = longname;)。< / p>

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

这是一个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();
          ^

1 个答案:

答案 0 :(得分:0)

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

所以这指出属性是&#34;只是在错误的地方&#34; (有趣的是,弃用警告后来仍按需要打印!)

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

第二个是我们想要的。