PrevUpHomeNext

std::cout utx::print


utx::print

utx::print is designed to output information that is more simple than std::cout. If you only want to print something simply, utx::print will be paid less. However if you want advanced interface with some other advanced streaming, use std::cout.

#include <utxcpp/core.hpp>

int main() {
	utx::print("Hello,", "c++", 20, '!');
}
Hello, c++ 20 !

std::cout

#include <iostream>

int main() {
	std::cout << "Hello," << "c++" << 20 << '!' << std::endl;
}
Hello,c++20!

std::cout advanced usage

operator<< overload

#include <iostream>
#include <utxcpp/core.hpp>

class Fizz {
private:
	utx::i32 value{123};
public:
	friend std::ostream & operator<<(std::ostream & out, const Fizz & f) {
		return out << f.value;
	}
};

int main() {
	Fizz f1, f2;
	std::cout << f1 << ' ' << f2 << '\n';
}
123 123

operator<< for utx::print

utx::print only accept printable value. If you want other values to be accepted by utx::print, you have to overload operator<< to let it printable. By the way, utx::print uses c++20 concepts to check if the value is printable. (aka utx::kspt::printable).

#include <iostream>
#include <utxcpp/core.hpp>

class Fizz {
private:
	utx::i32 value{123};
public:
	friend std::ostream & operator<<(std::ostream & out, const Fizz & f) {
		return out << f.value;
	}
};

int main() {
	Fizz f1, f2;
	utx::print(f1, f2);
}
123 123

utx::print and std::cout work together

utx::print_to is the middle underlying implementation for utx::print, it works with many c++ streams.

#include <utxcpp/core.hpp>
#include <complex>
#include <iostream>
#include <sstream>

using namespace std::complex_literals;

class Fizz {
private:
	const utx::string msg = "Object Fizz";
public:
	friend std::ostream & operator<<(std::ostream & out, const Fizz & fizz) {
		return out << std::quoted(fizz.msg);
	}
};

int main() {
	utx::string name = "Blitz";
	utx::i32 value = 123;
	std::complex<utx::f64> real = 3.2 + 5.9i;
	Fizz fizz;

	std::stringstream buffer;
	utx::print_to(buffer, name, value, real, fizz);

	std::cout << buffer.str() << '\n';
	std::cout << buffer.str().data() << '\n';
}
Blitz 123 (3.200000,5.900000) "Object Fizz"
Blitz 123 (3.200000,5.900000) "Object Fizz"

And this works too, (the same result):

utx::print_to(buffer, name, value, real, fizz);
utx::print(buffer.str());
utx::print(buffer.str().data());

Last revised: May 20, 2022 at 03:59:05 GMT


PrevUpHomeNext