PrevUpHomeNext

Method: .register_print


---- Method: .register_print << Output Concepts << Output << Kpp c++ ----

The method .register_print is a user-defined method of a class, it is constrained by the kpp::kspt::register_printable.

To meet kpp::kspt::register_printable, the following .register_print Signature is required:

class my_class
{
public:
	the_ostream_type & register_print(the_ostream_type & ostream__) const
	{
		// ... do real print
		// ... do real print
		return ostream__;
	}
};

Note:

  1. If T is a template argument, then T && have a change to be Lvalue reference, because & + && is &
  2. A variable type such as int can be used to simulate output stream type, this is accepted by kpp::kspt::register_printable

c++ example

#include <kpp/print.hpp>
#include <iostream>

namespace my_space
{

class my_class
{
private:
	std::string __data;
public:
	my_class(const std::string_view str__):
		__data{str__}
	{
	}
public:
	std::ostream & register_print(std::ostream & ostream__) const
	{
		ostream__ << __data;
		return ostream__;
	}
};

}

int main()
{
	my_space::my_class obj1{"Hello"};

	const auto obj2 = my_space::my_class{"c++"};
	static_assert(kpp::kspt::register_printable<decltype(obj1), std::ostream>);
	static_assert(kpp::kspt::register_printable<decltype(obj2), std::ostream>);

	static_assert(! kpp::kspt::register_printable<decltype(obj1), std::wostream>);
	static_assert(! kpp::kspt::register_printable<decltype(obj2), std::wostream>);

	std::cout << "end" << std::endl;
}

See Also

kpp::kspt::register_printable

kpp::print

Back

Up










Deck

Kpp Home


PrevUpHomeNext