我已经创建了 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/