Why deduction guide for std::array does not allow different types?

  • A+
Category:Languages

The deduction guide for std::array requires all types be the same:

std::array arr = { 1, 2, 3.4 }; // error 

What is the rationale behind such a requirement? Would there be any significant drawback if different types were allowed instead? For example:

namespace std {    template <typename... T>    array(T...) -> array<std::common_type_t<T...>, sizeof...(T)>; }  std::array arr = { 1, 2, 3.4 }; // decltype(arr)::value_type deduced as double 

 


It matches how function template arguments are deduced.

If they yield more than one possible deduced A, the type deduction fails.

[temp.deduct.call]/5

e.g.

template<typename T> void foo(T, T){}  template<typename T> struct bar{ bar(T, T) {} };  int main() {     foo(1, 1.5); // error, note:   deduced conflicting types for parameter 'T' ('int' and 'double')     bar(1, 1.5); // error, note:   deduced conflicting types for parameter 'T' ('int' and 'double') } 

But you can provide a deduction guide for common types.

template<typename T> struct baz{ baz(T, T) {} };  template<typename T, typename U> baz(T, U) -> baz<std::common_type_t<T, U>>     

or overloads that forward to common types

template<typename T> void quux(T, T){}  template<typename T, typename U> std::enable_if_t<!std::is_same<std::decay_t<T>, std::decay_t<U>>> quux(T t, U u)  {      using C = std::common_type_t<T, U>;      quux<C>(std::forward<C>(t), std::forward<C>(u)); // I think this is right }  int main() {     baz(1, 1.5);     quux(1, 1.5); } 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: