English 中文(简体)
[[可能——未使用]的奇金税在类型上的原因是什么?
原标题:What is the reason for the odd syntax of [[maybe_unused]] on type aliases?
  • 时间:2023-12-26 20:39:43
  •  标签:
  • c++
  • c++17

自C++17号文件以来,现在有一种标准化的方法,即从政治角度建议汇编者停止对未使用的实体进行销售。 在对变数、功能和旧的ool类别进行应用时,我们必须使用预期的yn,其形式是:

[[maybe_unused]] int x;
[[maybe_unused]] void f();
[[maybe_unused]] typedef void t;

这里没有任何奇怪之处。 然而,在将这一特性——投机者——用于制造、 en,特别是现代的类型——时,我们必须诉诸于各种 go,使其发挥作用:

struct [[maybe_unused]] s;
union [[maybe_unused]] u;
enum class [[maybe_unused]] e;
using a [[maybe_unused]] = void; // what s up with this?

The What is theêt for the odd place of the [[maybe_un used] Depende for category-aliases? 这难道不是任何其他方式吗?

https://godbolt.org/z/6jq11c91P”rel=“nofollow noreferer”>

问题回答

由于它就像一个螺旋式:内部属性适用于内部名称/型号,外部属性适用于外名。

   [[maybe_unused]] struct [[maybe_unused]] s {} s_;
//                         ^^^^^^^^^^^^^^^^-^
//                         applied to the type
//
// ^^^^^^^^^^^^^^^^------------------------------^^
// applied to the variable       
[[maybe_unused]] struct s1;     // invalid, there is no variable name
[[maybe_unused]] struct {} s2;  // valid, there is a variable name

As for the alias-declaration:
    using identifier [attribute-specifier-seq] = defining-type-id ;
no clue how to justify that.

The ability to place an attribute-specifier-seq in an alias declaration was added by the resolution to CWG1042. Unfortunately, when I looked through the CWG minutes, I couldn t find any recorded discussion about the placement of the attribute-specifier-seq. But it s pretty easy to come up with a guess as to why it goes after the identifier: to mirror the syntax of a typedef declaration:

typedef void a [[maybe_unused]];

一类声明是简单申报<>,即其结构与一个或多个变数或功能的申报相同。 www.un.org/Depts/DGACM/index_french.htm

int f [[deprecated]] (), x [[maybe_unused]];

请注意,<代码>f为未经论证和返回<代码>int和x可能是有意未使用的<代码>t的变式。

您也可在“简单明了/em>的开头处做出说明,但在这种情况下,它只向“all在声明中宣布的实体做了说明,而在申报人-id之后,将其属性仅传给单一实体。

Since an alias-declaration only declares one entity at a time, the hypothetical syntax

[[maybe_unused]] using a = void;

would have been reasonable too, and would have the same meaning; perhaps CWG simply did not wish to provide two ways of writing the same thing.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签