Coder Social home page Coder Social logo

print-stmt's Introduction

An implementation of a print statement in C++.

#include "print.h"

print expression[, expression]*
print "format" % expression[, expression]*
eprint expression[, expression]*
eprint "format" % expression[, expression]*

/** examples **/
print "Hello", "world", time(0);       // Hello world 1360826578
print "name=%q age=%s" % "Alex", 26;   // name="Alex" age=26

int numbers[] = { 1, 2, 3 };
print numbers;                         // [1, 2, 3]

std::vector<std::string> things = {"apples", "oranges", "elephants"};
print things;                          // [apples, oranges, elephants]

The first form converts each expression into a string and prints it to standard output, with each expression separated by a space and a terminating newline at the end. eprint is similar except it prints to standard error.

The second form uses a format string similar to printf().

  • %s The argument is printed directly.
  • %q The argument is quoted.

Expressions are converted to strings according to their type, as follows:

  • char* are output as NUL-terminated strings like %s.
  • int, short, long, signed char, unsigned char are printed in decimal notation like with %d.
  • char prints the charcter like %c.
  • void * or any other kind of pointers prints ths pointer in hex like %p.
  • double, float prints the number in decimal notation like %g.
  • bool prints 'true' or 'false'
  • A type that defines a member function called c_str() is converted to a string by calling this function.
  • A type than can be streamed to an std::ostream like std::cout << thing but does not have a c_str() member function is converted to a string using <<.
  • A type that has a begin() member function that returns in iterator but does not have a c_str() member function is printed like a list, e.g. ["one", "two"].
  • std:map, std::unordered_map, std::multimap, std::unordered_multipmap, QHash, QMap, QMultiHash, QMultiMap are special-cased and printed like dictionaries, e.g. {"foo": "bar"}.
  • QString is printed as a string.
  • std::pair is rendered as ("first", "second").

Usage

The easiest way to use this is to clone this repo and copy the file print.h into your project. Then simply

#include "print.h"` 

at the top of the file, but after the system #includess.

Misc

print is thread and exception safe. The output stream is locked once before anything is printed and then unlocked at the end inside a destructor. This ensures that the whole line will be printed without being interleaved with output from another thread.

Internally, the fwrite() family of function from C standard library are used for output. C++ streams are not used at all.

In benchmakrs print is a little bit faster than cout and a little bit slower than printf.

print.h includes stdio.h and nothing else.

print-stmt's People

Contributors

adob avatar bryant1410 avatar torvalds avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

print-stmt's Issues

Giving 20 errors when trying to add your library

Like in subject.
./print.h:62:13: error: no template named 'basic_ostream'
typedef basic_ostream< char, char_traits > ostream;
^
./print.h:62:34: error: reference to 'char_traits' is ambiguous
typedef basic_ostream< char, char_traits > ostream;
^
./print.h:54:12: note: candidate found by name lookup is 'std::char_traits'
struct char_traits;
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/__string:75:29: note: candidate found by name lookup
is 'std::__1::char_traits'
struct _LIBCPP_TEMPLATE_VIS char_traits
^
In file included from Alg.cpp:4:
./print.h:62:50: error: expected '(' for function-style cast or type construction
typedef basic_ostream< char, char_traits > ostream;
~~~~^
./print.h:62:52: error: expected unqualified-id
typedef basic_ostream< char, char_traits > ostream;
^
./print.h:63:13: error: no template named 'basic_streambuf'; did you mean 'basic_stringbuf'?
typedef basic_streambuf< char, char_traits > streambuf;
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/iosfwd:117:32: note: 'basic_stringbuf' declared here
class _LIBCPP_TEMPLATE_VIS basic_stringbuf;
^
In file included from Alg.cpp:4:
./print.h:63:36: error: reference to 'char_traits' is ambiguous
typedef basic_streambuf< char, char_traits > streambuf;
^
./print.h:54:12: note: candidate found by name lookup is 'std::char_traits'
struct char_traits;
^
/Library/Developer/CommandLineTools/usr/include/c++/v1/__string:75:29: note: candidate found by name lookup
is 'std::__1::char_traits'
struct _LIBCPP_TEMPLATE_VIS char_traits
^
In file included from Alg.cpp:4:
./print.h:63:52: error: expected '(' for function-style cast or type construction
typedef basic_streambuf< char, char_traits > streambuf;
~~~~^
./print.h:63:54: error: expected unqualified-id
typedef basic_streambuf< char, char_traits > streambuf;
^
./print.h:81:13: error: use of undeclared identifier 'fputs_unlocked'; did you mean 'putc_unlocked'?
fputs_unlocked("\r", file); break;
^~~~~~~~~~~~~~
putc_unlocked
/usr/include/stdio.h:295:6: note: 'putc_unlocked' declared here
int putc_unlocked(int, FILE *);
^
In file included from Alg.cpp:4:
./print.h:81:28: error: cannot initialize a parameter of type 'int' with an lvalue of type 'const char [3]'
fputs_unlocked("\r", file); break;
^~~~~
/usr/include/stdio.h:295:23: note: passing argument to parameter here
int putc_unlocked(int, FILE *);
^
In file included from Alg.cpp:4:
./print.h:83:13: error: use of undeclared identifier 'fputs_unlocked'; did you mean 'putc_unlocked'?
fputs_unlocked("\n", file); break;
^~~~~~~~~~~~~~
putc_unlocked
/usr/include/stdio.h:295:6: note: 'putc_unlocked' declared here
int putc_unlocked(int, FILE *);
^
In file included from Alg.cpp:4:
./print.h:83:28: error: cannot initialize a parameter of type 'int' with an lvalue of type 'const char [3]'
fputs_unlocked("\n", file); break;
^~~~~
/usr/include/stdio.h:295:23: note: passing argument to parameter here
int putc_unlocked(int, FILE *);
^
In file included from Alg.cpp:4:
./print.h:85:13: error: use of undeclared identifier 'fputs_unlocked'; did you mean 'putc_unlocked'?
fputs_unlocked("\t", file); break;
^~~~~~~~~~~~~~
putc_unlocked
/usr/include/stdio.h:295:6: note: 'putc_unlocked' declared here
int putc_unlocked(int, FILE *);
^
In file included from Alg.cpp:4:
./print.h:85:28: error: cannot initialize a parameter of type 'int' with an lvalue of type 'const char [3]'
fputs_unlocked("\t", file); break;
^~~~~
/usr/include/stdio.h:295:23: note: passing argument to parameter here
int putc_unlocked(int, FILE *);
^
In file included from Alg.cpp:4:
./print.h:87:13: error: use of undeclared identifier 'fputs_unlocked'; did you mean 'putc_unlocked'?
fputs_unlocked("\\", file); break;
^~~~~~~~~~~~~~
putc_unlocked
/usr/include/stdio.h:295:6: note: 'putc_unlocked' declared here
int putc_unlocked(int, FILE *);
^
In file included from Alg.cpp:4:
./print.h:87:28: error: cannot initialize a parameter of type 'int' with an lvalue of type 'const char [3]'
fputs_unlocked("\\", file); break;
^~~~~~
/usr/include/stdio.h:295:23: note: passing argument to parameter here
int putc_unlocked(int, FILE *);
^
In file included from Alg.cpp:4:
./print.h:89:13: error: use of undeclared identifier 'fputs_unlocked'; did you mean 'putc_unlocked'?
fputs_unlocked("\"", file); break;
^~~~~~~~~~~~~~
putc_unlocked
/usr/include/stdio.h:295:6: note: 'putc_unlocked' declared here
int putc_unlocked(int, FILE *);
^
In file included from Alg.cpp:4:
./print.h:89:28: error: cannot initialize a parameter of type 'int' with an lvalue of type 'const char [3]'
fputs_unlocked("\"", file); break;
^~~~~~
/usr/include/stdio.h:295:23: note: passing argument to parameter here
int putc_unlocked(int, FILE *);
^
In file included from Alg.cpp:4:
./print.h:91:13: error: use of undeclared identifier 'fputs_unlocked'; did you mean 'putc_unlocked'?
fputs_unlocked("\0", file); break;
^~~~~~~~~~~~~~
putc_unlocked
/usr/include/stdio.h:295:6: note: 'putc_unlocked' declared here
int putc_unlocked(int, FILE *);
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
1 warning and 20 errors generated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.