Overload uncertainty in C ++ for automatically converting objects to the print format

I am trying to write a function that accepts input. If this input can be directly passed to the stream (for example, using std::cout <<), it returns the input unchanged. Otherwise, it tries to convert the input to a string and returns a string.

I have the following code:

//Uses SFINAE to determine which overload to call
//See: https://en.wikipedia.org/wiki/Substitution_failure_is_not_an_error
//Basically, make_printable detects whether an object can be directly piped to a stream.
//If it can't be piped to a stream, it converted into a string.
template<typename T, 
        typename StreamT = void, 
        typename = decltype(std::declval<T>().operator std::string())>
std::string make_printable(const T& obj) {
    std::cout << "[std::string make_printable(obj)]";
    return (std::string)obj;
}

template<
        typename T, 
        typename StreamT = std::ostream,
        typename = decltype(std::declval<StreamT&>() << std::declval<T const &>())>
const T& make_printable(const T& obj) {
    std::cout << "[const T& make_printable(obj)]";
    return obj;
}

This code works when calling objects that can be converted to a string or can be written to a stream, but if I have an object that can be converted to a string and written to a stream, the code does not work due to ambiguity as to which call function.

, , , -?

+4
3

, , , -?

, .

... int long

template<typename T, 
        typename StreamT = void, 
        typename = decltype(std::declval<T>().operator std::string())>
std::string make_printable (T const & obj, long)
 {                                     //  ^^^^ <-- long argument
   std::cout << "[std::string make_printable(obj)]";
   return (std::string)obj;
 }

template<
        typename T, 
        typename StreamT = std::ostream,
        typename = decltype(std::declval<StreamT&>() << std::declval<T const &>())>
T const & make_printable (T const & obj, int)
 {                                    // ^^^ <-- int argument
   std::cout << "[const T& make_printable(obj)]";
   return obj;
 }

make_printable(), int

template <typename T>
auto make_printable (T const & obj)
 { return make_printable(obj, 0); }

, , int int, a long.

, .

En passant: auto ... -> decltype() , SFINAE .

template <typename T>
auto make_printable (T const & obj, long)
   -> decltype( obj.operator std::string() )
 {
   std::cout << "[std::string make_printable(obj)]";
   return (std::string)obj;
 }
template <typename T, typename StreamT = std::ostream>
auto make_printable (T const & obj, int)
   -> decltype( std::declval<StreamT>() << obj , obj )
 {
   std::cout << "[const T& make_printable(obj)]";
   return obj;
 }

, , .

+1

, . SFINAE. ( - std::string(std::declval<T>()), .)

SFINAE string, , . (.. ), , .

, : , , SFINAE:

template<class T,class=std::ostream&>
struct printable {
  std::string make(const T& obj) {
    std::cout << "[std::string make_printable(obj)]";
    return (std::string)obj;
  }
};

template<typename T>
struct printable<T,decltype(std::declval<std::ostream&>() << std::declval<T const &>())> {
  const T& make(const T& obj) {
    std::cout << "[const T& make_printable(obj)]";
    return obj;
  }
};

template<class T>
auto make_printable(const T &t)
{return printable<T>::make(t);}

().

0

std::declval<StreamT&>() << std::declval<T const &>() bool, .

template <typename T, 
          typename StreamT, 
          typename = void>
struct Can_output_directly : std::false_type {};

template <typename T, 
          typename StreamT>
struct Can_output_directly<T, StreamT, std::void_t<decltype(std::declval<StreamT&>() << std::declval<T const &>())>> : std::true_type {};
                                    // ^^^^^^^^^^^ C++17 feature

make_printable:

template <typename T, 
          typename StreamT = std::ostream, // void -> std::ostream
          typename = decltype(std::declval<T>().operator std::string()),
          typename std::enable_if<!Can_output_directly<T, StreamT>::value, int>::type = 0> // note this parameter
std::string make_printable(const T& obj) {
    std::cout << "[std::string make_printable(obj)]";
    return (std::string)obj;
}

, Can_output_directly<T, StreamT>::value - false, .. .

0
source

Source: https://habr.com/ru/post/1694380/


All Articles