should std::common_type use std::decay?

前端 未结 1 366
面向向阳花
面向向阳花 2020-12-01 16:36

Given types A,B, I am concerned with the exact definition of std::common_type, disregarding the variadic case std::common_type<

相关标签:
1条回答
  • 2020-12-01 17:04

    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

    0 讨论(0)
提交回复
热议问题