简体   繁体   English

std::decay 和删除 const 限定符

[英]std::decay and removing const qualifiers

I'm trying to understand the workings of std::decay a little better.我试图更好地理解 std::decay 的工作原理。 Per cppreference it's supposed to remove const and volatile classification from the type as part of other transformations it does.每个 cppreference 它应该从类型中删除 const 和 volatile 分类,作为它所做的其他转换的一部分。 However, below function displays "False", "True" vs. "True", "True" as one might expect.但是,在 function 下方显示“假”、“真”与“真”、“真”,正如人们所期望的那样。 Can someone please clarify why the const is needed when matching against the decayed type here?有人可以澄清为什么在与衰减类型匹配时需要 const 吗?

int main()
{
   const char *p = "testing";
   cout << "------------------" << endl;
   cout << boolalpha << is_same<char *, decay_t<decltype(p)>>::value << endl;
   cout << boolalpha << is_same<const char *, decay_t<decltype(p)>>::value << endl;
   cout << "------------------" << endl;
}

std::decay_t would remove the const from the pointer, had it been const , not the type it's pointing at. std::decay_t将从指针中删除const ,如果它是const ,而不是它指向的类型。

That is, a char* const would decay into a char* .也就是说, char* const会衰减为char*

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 std :: variant转换构造函数无法处理const volatile限定符 - std::variant converting constructor doesn't handle const volatile qualifiers 错误:传递&#39;const string {aka const std::basic_string<char> }&#39; 作为 ... 的 &#39;this&#39; 参数丢弃限定符 [-fpermissive] - error: passing ‘const string {aka const std::basic_string<char>}’ as ‘this’ argument of ...discards qualifiers [-fpermissive] 将&#39;const Link&#39;传递为&#39;std :: string GetAttribute(std :: string)&#39;的&#39;this&#39;参数时,将丢弃限定符 - passing ‘const Link’ as ‘this’ argument of ‘std::string GetAttribute(std::string)’ discards qualifiers const挥发性限定词的丢失 - loss of const volatile qualifiers Const限定符和&符号 - Const qualifiers and ampersand const字符串丢弃限定符 - const string discards qualifiers const std :: unordered_map <char, int> &#39;as&#39;this&#39;参数将丢弃lambda中的限定词 - const std::unordered_map<char, int>’ as ‘this’ argument discards qualifiers in lambda C ++错误:传递&#39;const std :: vector <Node> &#39;作为&#39;this&#39;参数丢弃限定词[-fpermissive] - c++ error: passing ‘const std::vector<Node>’ as ‘this’ argument discards qualifiers [-fpermissive] c++“错误:传递'const std::vector<int> ' 因为 'this' 参数丢弃了限定符 [-fpermissive]”</int> - c++ “error:passing 'const std::vector<int>' as 'this' argument discards qualifiers [-fpermissive]” 错误:传递'const std::unordered_map<char, int> ' 作为 'this' 参数丢弃限定符</char,> - error: passing ‘const std::unordered_map<char, int>’ as ‘this’ argument discards qualifiers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM