search

Home  >  Q&A  >  body text

c++ - 为什么add_const<T>中的T若为引用则add_const<T>::type仍为T?

为什么add_const对引用无效,比如我认为,add_const<int &>::type应该是const int &才对,
而实际上,add_const<int &>::type仍旧是int &

阿神阿神2803 days ago604

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-17 13:15:34

    References only have underlying const, not top-level const.
    If add_const<int &> changes int &, its type will be int &const, which is obviously wrong

    reply
    0
  • 阿神

    阿神2017-04-17 13:15:34

    template <typename T, 
        bool = is_reference<T>::value ||
        is_function<T>::value ||
        is_const<T>::value
    >
    struct add_const;

    Then I wrote specializations of true and false

    To add a reference, there are one add_lvalue_reference and add_rvalue_reference

    reply
    0
  • Cancelreply