English 中文(简体)
因此,将阵列第1部分的地址作为非类型示范理由在C++中是可能的
原标题:why using the address of the first element of the array as template non-type argument is possible in c++ 17
问题回答

<>斯特隆>;

答案取决于哪一种标准C++版本是你重新使用的。 值得注意的是,a has staticstordur;c++20允许<><><>>>>>>>。

C++20

该方案是https://eel.is/c++draft/temp.arg#nontype-6”rel=“nofollow noreferer”>temp.arg.nontype :

For a non-type template-parameter of reference or pointer type, or for each non-static data member of reference or pointer type in a non-type template-parameter of class type or subobject thereof, the reference or pointer value shall not refer to or be the address of (respectively):

  • a temporary object
  • a string literal object
  • the result of a typeid expression
  • a predefined func variable or
  • a subobject (6.7.2) of one of the above.

(斜体地雷)

Since &a[0] is neither of the above things mentioned in the list, the program is well-formed.

还指出,模板论点必须是未标明的经常表述,此处确实如此。 https://timsong-cpp.github.io/cppwp/n4861/temp.arg#nontype-2”rel=“nofollow noreferer”>temp.arg:

A template-argument for a non-type template-parameter shall be a converted constant expression ([expr.const]) of the type of the template-parameter. [ Note: If the template-argument is an overload set (or the address of such, including forming a pointer-to-member), the matching function is selected from the set ([over.over]). — end note ]

A converted constant expression of type T is an expression, implicitly converted to type T, where the converted expression is a constant expression and the implicit conversion sequence contains only : * ...

我们现在走到constant expression

1. 经常表达是指通过不断表达(如下定义)而允许的某一实体,或指一种精细的核心不变表述,其价值取决于以下制约因素:

  • if the value is of pointer type, it contains the address of an object with static storage duration, the address past the end of such an object ([expr.add]), the address of a non-immediate function, or a null pointer value,

C++17

在C++17,该方案按temp.arg:

A template-argument for a non-type template-parameter shall be a converted constant expression of the type of the template-parameter. For a non-type template-parameter of reference or pointer type, the value of the constant expression shall not refer to (or for a pointer type, shall not be the address of):

  • a subobject,

(斜体地雷)





相关问题
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?

热门标签