c++ - 属性 "deprecated"到 C++17 中的命名空间

标签 c++ attributes c++17

我已经创建了 C++ 程序来试验 C++17 的 deprecated 关键字。

它在 clang(3.8.0) 中工作正常,但是,g++(5.4.0) 给出了一个错误。

source_file.cpp:9:11: error: expected identifier before ‘[’ token
 namespace [[ deprecated ]] bar
           ^
source_file.cpp:9:28: error: ‘bar’ does not name a type
 namespace [[ deprecated ]] bar
                            ^
source_file.cpp: In function ‘int main()’:
source_file.cpp:16:5: error: ‘bar’ has not been declared
     bar::var = 10;

为什么?

#include <iostream>
using namespace std;

namespace foo  
{ 
  int var; 
}

namespace [[ deprecated ]] bar
{ 
  int var; 
}

int main() 
{
    bar::var = 10;
    return 0;
}

最佳答案

线索在错误消息中:“[ token 之前需要标识符”。 标识符bar,它必须出现在之前 [。所以:

namespace bar [[deprecated]]
{ } 

使用 GCC6.3 测试

关于c++ - 属性 "deprecated"到 C++17 中的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46052410/

相关文章:

c++ - 共享库或 dylib 中 undefined symbol 的处理方式是什么?

c++ - 's the difference between application' 是什么输入点地址和可执行起始地址

jsf - 自定义 JSF 组件属性列表

c++ - 生成编译时数组结构 (c++17)

c++ - 遍历 C++ 17 中的映射键

c++ - 提示编译器在使用 auto 时按引用返回,而不是使用 ->

c++ - 当需要指向全局函数的指针时如何使用指向成员函数的指针?

循环中的 C++ 对象引用

asp.net-mvc - 带有ASP.NET MVC预览5的Html.TextBox条件属性

asp.net - 将动态变量传递给 UserControl 属性