lundi 9 décembre 2019

Curious results from decltype( std::devlcal

Consider the following struct:

template<typename T>
struct stream
{
  using type = decltype(
      std::declval<std::ostream>() << std::declval<T>()
    );
};

template<typename T>
using stream_t = stream<T>::type;

The "value" of stream_t<T> when using certain built-in types (int, float, ...) for T is std::ostream&, as I expected.

But when using std::string, char, int*, or some streamable dummy struct for T, the type is an rvalue reference, std::ostream&&.

Once std::declval<std::ostream>() (returns an std::ostream&&) is replaced withstd::declval<std::ostream&> (returns an std::ostream&, due to reference collapsing rule, right?) the returned type is the expected std::ostream&. Is there some rvalue overload of operator<< that I don't know about?

Why is this happening?

Aucun commentaire:

Enregistrer un commentaire