Skip to content

Avoid always define an external friend function to streamlize an object in the c++ style by using template.

Notifications You must be signed in to change notification settings

Jin-W-FS/cpp-in-class-streamlize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

cpp-in-class-streamlize

For avoiding defining an external friend function to streamlize an object in c++ style.

Thus: instead of writing code like this:

// XXX.hpp
class C {
    ...
public:
    friend std::ostream& operator<<(std::ostream&, const C&);
};

// XXX.cpp or with "static inline" in XXX.hpp; outof the class, anyway.
std::ostream& operator<<(std::ostream& os, C& obj) {
    ...      	      
}

we could write like this:

// XXX.hpp
#include "streamlize.hpp"

class C {
    ...
public:
    __to_stream__(std::ostream& os) {
        ...
    }
};

and when calling, say

	std::cout << C();

it will automatically call that function, just like str or len functions in Python.

About

Avoid always define an external friend function to streamlize an object in the c++ style by using template.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages