0

This question was asked to me in an interview. I am still searching for the solution of this question.

The following two types of functions (both or one) can be present inside a concrete class:

  1. ostream& prettyPrint(ostream& ost) const;
    
  2. std::string toString() const;
    

Our goal is to design a template Class PrettyPrint which support both kinds of implementation.

PrettyPrint uses functions of concrete class in the priority I have written them: if both are present, the first will be used, else the second.

    // ******* given code - start ********

    struct LoginDetails {
        std::string m_username;
        std::string m_password;

        std::string toString() const {
            return "Username: " + m_username
            + " Password: " + m_password;
        }
    };

    struct UserProfile {
        std::string m_name;
        std::string m_email;

        ostream& prettyPrint(ostream& ost) const {
            ost << "Name: " << m_name
            << " Email: " << m_email;
            return ost;
        }

        std::string toString() const {
            return "NULLSTRING";
        }
    };
    // ******* given code - end ********

    // Implement PrettyPrint Class
    template <class T>
    class PrettyPrint {
    public:
        PrettyPrint(){

        }
    };

    int main() {

        LoginDetails ld = { "Android", "Apple" };
        UserProfile  up = { "James Bond", "james@bond.com" };

        // this should print "Name: James Email: james@bond.com"
        std::cout << PrettyPrint <UserProfile> (up) << std::endl;

        // this should print "Username: Android Password: Apple"
        std::cout << PrettyPrint <LoginDetails> (ld) << std::endl;
    }
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Anurag
  • 56
  • 1
  • 6

1 Answers1

0
template<typename T>
struct HasPrettyPrintMethod
{
    template<typename U, std::ostream&(U::*)(std::ostream&) const> struct SFINAE {};
    template<typename U> static char Test(SFINAE<U, &U::prettyPrint>*);
    template<typename U> static int Test(...);
    static const bool Has = sizeof(Test<T>(0)) == sizeof(char);
};

template <class T>
class PrettyPrint {
public:
    PrettyPrint(T m)
    {
        CallPrint(m, std::integral_constant<bool, HasPrettyPrintMethod<T>::Has>());
    }
    void CallPrint(const T& m, std::true_type)
    {
        std::ostringstream  os;
        m.prettyPrint(os);
        buf = os.str();
    }
    void CallPrint(const T& m, std::false_type)
    {
        buf = m.toString();
    }
    std::string buf;
};
template <class T>
std::ostream& operator<<(std::ostream &os, PrettyPrint<T> const &m)
{
    return os << m.buf;
}

ref:Check if a class has a member function of a given signature

Community
  • 1
  • 1
bottlerun
  • 51
  • 6