英文:
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<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';
> 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 *
中,const
和volatile
指的是int
,而不是指针。 remove_cv
影响类型本身的限定符,而在您的情况下,类型是一个没有限定符的指针。 因此,显示的行为是正确的。 如果您希望const
和volatile
影响指针而不是指向的类型,则您对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...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论