关于类型特性 std::remove_cv 的问题。

huangapple go评论62阅读模式
英文:

A question about type traits std::remove_cv

问题

我的参考是下面提供的示例:

std::remove_cv、std::remove_const、std::remove_volatile

在示例中,

using type4 = std::remove_cv<const volatile int*>::type;
using type5 = std::remove_cv<int* const volatile>::type;

std::cout << std::is_same<type4, int*>::value << ' '
          << std::is_same<type4, const volatile int*>::value << '\n';

std::cout << std::is_same<type5, int*>::value << '\n';

输出

true false

true

我假设输出中有一个打字错误,因为它与示例中的测试不匹配,如果我对这个概念的理解是正确的话。输出应该是

true false

true

有人能确认或更正吗?

谢谢。

英文:

My reference is to the example provided hereunder:

std::remove_cv, std::remove_const, std::remove_volatile

In the example,

using type4 = std::remove_cv&lt;const volatile int*&gt;::type;
using type5 = std::remove_cv&lt;int* const volatile&gt;::type;

std::cout &lt;&lt; std::is_same&lt;type4, int*&gt;::value &lt;&lt; &#39; &#39;
              &lt;&lt; std::is_same&lt;type4, const volatile int*&gt;::value &lt;&lt; &#39;\n&#39;;

    std::cout &lt;&lt; std::is_same&lt;type5, int*&gt;::value &lt;&lt; &#39;\n&#39;;

> Output
>
> false true
>
> true

I am assuming that there is a typo in the output as it doesn't match the test in the example, if my understanding of the concept is correct. The output instead should have been

> true false
>
> true

Can someone confirm or correct this?

TIA

答案1

得分: 1

const volatile int *中,constvolatile指的是int,而不是指针。 remove_cv 影响类型本身的限定符,而在您的情况下,类型是一个没有限定符的指针。 因此,显示的行为是正确的。 如果您希望constvolatile影响指针而不是指向的类型,则您对type5使用的构造是正确的。 如果您希望remove_cv影响指向的类型,您将不得不手动删除指针并重新添加。

英文:

In const volatile int *, const and volatile refer to int, not the pointer. remove_cv affects the qualifiers of the type itself, which is a pointer without any qualifiers in your case. So the displayed behavior is correct. If you want const and volatile to affect the pointer rather than the pointed-to type, the construct you used for type5 is correct. If you want remove_cv to affect the pointed-to type, you'll have to remove the pointer and add it back in manually

答案2

得分: 0

在链接的文档中,在示例的上方有如下说明:

"> 从const volatile int *中移除const/volatile不会修改类型,因为指针本身既不是const也不是volatile。"

(也许可以在这方面更清楚地提供示例……)

英文:

In the linked document, just above the example it states:

> Removing const/volatile from const volatile int * does not modify the type, because the pointer itself is neither const nor volatile.

(Perhaps the example could be made clearer on this...)

huangapple
  • 本文由 发表于 2023年7月20日 18:22:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定