zgbzsu2008 2019-01-24 17:34 采纳率: 0%
浏览 321

std::point_traits::element_type 等价于 boost::pointee::type

看书上说std::point_traits::element_type 等价于 boost::pointee::type
但是

 std::is_same<boost::pointee<std::string::iterator>::type, char>::value // true
std::is_same<std::pointer_traits<std::string::iterator>::element_type, char>::value // false

为啥一个结果true 一个结果false

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-07 19:16
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    boost::pointee返回的是std::string::iterator对象的值,所以它返回char类型。而std::point_traits::element_type返回的是std::string::iterator对象指向元素的类型,也就是char

    如果你在使用std::is_same检查这些类型是否相等,那么std::is_same<...>会比较两个类型的值,并且如果它们不相等就会返回false。因此,在这种情况下,这两个函数的结果不同是正常的。 例如:

    #include <iostream>
    #include <string>
    
    int main() {
        std::cout << (std::is_same<char, boost::pointee<std::string::iterator>::type>::value) ? "true" : "false" << std::endl;
        std::cout << (std::is_same<char, std::point_traits<std::string::iterator>::element_type>::value) ? "true" : "false" << std::endl;
    }
    

    输出:

    true
    false
    
    评论

报告相同问题?