should std::common_type use std::decay?

萝らか妹 提交于 2019-11-27 08:54:05

should std::common_type use std::decay?

Yes, see Library Working Group Defect #2141.

Short version (long version, see link above):

  • declval<A>() returns a A&&

  • common_type is specified via declval, n3337:

    template <class T, class U>
    struct common_type<T, U> {
        typedef decltype(true ? declval<T>() : declval<U>()) type;
    };
    
  • common_type<int, int>::type therefore yields int&&, which is unexpected

  • proposed resolution is to add decay

    template <class T, class U>
    struct common_type<T, U> {
        typedef decay_t < decltype(true ? declval<T>() : declval<U>()) > type;
    };
    
  • common_type<int, int>::type now yields int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!