mercredi 29 avril 2020

Why does this c++ template scenario unable to compile:

struct stream_type1 {
  template<typename T>
  const T& read() const;
};

struct stream_type2 {
  template<typename T>
  const T& read() const;
};

template<typename S, typename T>
const T& stream_read(const S& stream)
{
  return stream.read<T>();
}

// example:
stream_type1 stream1;
stream_type1 stream2;

int value1 = stream_read<int>(stream1);
int value2 = stream_read<int>(stream2);

error: C2665: 'stream_read': none of the 2 overloads could convert all the argument types

so, I have to specialize the template witch makes it redundant

template<typename T>
const T& stream_read(const stream_type1 & stream)
{
  return stream.read<T>();
}

template<typename T>
const T& stream_read(const stream_type2 & stream)
{
  return stream.read<T>();
}

Aucun commentaire:

Enregistrer un commentaire