gpt4 book ai didi

c++ - Visual Studio 2017 是否完全支持具有 [[deprecated]] 属性的 N4266?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:48 24 4
gpt4 key购买 nike

我目前正在研究 C++17 的新特性。我偶然发现了这个功能 N4266 ,它指出现在枚举和命名空间也可以使用属性。不同消息来源称,Visual Studio 2017 已经完全支持该功能。我用 [[deprecated]] 属性编写了一个测试。对于 namespace ,这非常有效。但是,不会为枚举生成警告。我的实现有错误吗?我错过了什么吗?

enum MyEnum
{
val = 0,
vaal[[deprecated]] = val
};

void test()
{
MyEnum e = MyEnum::vaal; //Should emit Warning, but does not
MyEnum e2 = MyEnum::val; //No Warning
}

我使用的是 Visual Studio Community 2017 版本 15.3.5。 This声明自 VS2015 以来就应该支持它。/std:c++17 被使用。

And this还说这应该是正确的语法。

使用 enum 或 enum class 没有区别。

最佳答案

不,不完全是。

我想说这是 MSVC2017 中的当前限制/错误,可能值得提交错误报告,即使在枚举器案例中也可以识别该属性(并且不会提示 C5030 警告;如 Mark 所述在对你问题的评论中)。

附录:现已通过 Hans Passant 提供的链接验证(作为先前已知的错误):


C++17(工作草案)标准,[decl.attr.deprecated] , 状态:

§1 The attribute-token deprecated can be used to mark names and entities whose use is still allowed, but is discouraged for some reason.

§3 The attribute may be applied to the declaration of a class, a typedef-name, a variable, a non-static data member, a function, a namespace, an enumeration, an enumerator, or a template specialization. ...

在使用 MSVC2017 编译时使用 /std:c++17 标志,deprecated 属性适用于除枚举器之外的所有上述内容:

// a class
class [[deprecated]] MyClass {};

// a typedef-name / type alias
[[deprecated]] typedef int MyInt;
using MyFloat [[deprecated]] = float;

// a variable (see main)

// a non-static data member
struct DataMember
{
int b [[deprecated]];
};

// a function
[[deprecated]]
void myFunction() {}

// a namespace
namespace [[deprecated]] my_namespace
{
typedef double MyDouble;
}

// an enumeration
enum [[deprecated]] MyEnum {};

// an enumeration
enum MyNewEnum
{
val[[deprecated]] = 42
};

// a template specialization
template <typename T>
void myTemplateFunction(const T) {}

template<>
[[deprecated]]
void myTemplateFunction<int>(const int) {}

int main()
{
MyClass m; // warning C4996: 'MyClass': was declared deprecated
MyInt i; // warning C4996: 'MyInt': was declared deprecated
MyFloat f; // warning C4996: 'MyInt': was declared deprecated
int j [[deprecated]];
j = 1; // warning C4996: 'j': was declared deprecated
DataMember dm;
dm.b = 1; // warning C4996: 'DataMember::b': was declared deprecated
myFunction();
// warning C4996: 'myFunction': was declared deprecated
my_namespace::MyDouble d;
// warning C4996: 'my_namespace': was declared deprecated
MyEnum e; // warning C4996: 'MyEnum': was declared deprecated
myTemplateFunction(2);
// warning C4996: 'myTemplateFunction': was declared deprecated

MyNewEnum ne = val; // ... no warning

return 0;
}

gcc 和 clang 都会为上面的 MyNewEnum ne = val;-std=c++14-std=c 调用弃用警告++1z.

在相关说明中,特别是 cppreference's description of the deprecated attribute似乎没有用 N4266 更新,在有效用例声明中不包括“枚举器”和“命名空间”;即使这些都存在于 C++14 standard draft (7.6.5/2) 中.这可能表明这是一个很少使用的功能(应用于枚举器/命名空间),这可以解释为什么它在 MVCS 中被(部分)遗漏了。

  • 编辑:添加了关于此的注释,此后已更正。

官方 MVSC 文档中的一些歧义

最后,attributes section in the MSVC docs并没有真正深入地指定允许使用 deprecated 属性的声明类型;仅显示弃用函数声明的示例。

虽然,正如您所指出的(但是通过指向 cppreference 的链接而不是 MSVC 自己的文档),MSVS2015 does explicitly state conformance to N4266 ,至少对于 C++17。

Compiler Features

C++17 Core Language Features

N4266 Attributes for namespaces and enumerators

Supported: VS2015

然而,Support For C++11/14/17 Features (Modern C++)相反的情况(对于 VS2015):

Compiler Features

C++17 Proposed Core Language Features

N4266 Attributes for namespaces and enumerators

Supported VS2013: No

Supported VS2015: No

关于c++ - Visual Studio 2017 是否完全支持具有 [[deprecated]] 属性的 N4266?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46688604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com