/luple

Home to luple, nuple, C++ String Interning, Struct Reader and C++ Type Loophole

Primary LanguageC++

This repository is home to separate but related projects: C++ Type Loophole, luple, nuple, C++ String Interning, Struct Reader.

luple: A Lightweight (in terms of source code) Tuple (C++14)

Header file: luple.h

Luple is a tuple-like data structure. Its primary advantage is that it's very thin and also non-recursive (uses multiple inheritance). std::tuple is usually implemented as a single inheritance hierarchy and as a result its layout can vary between compilers (see this post). Also it stores data in reverse order. Luple is built on multiple inheritance and its layout is consistent among compilers. It has the same layout as a non-virtual flat data structure with the same data members (formally luple is not a POD and we should keep that in mind when we reinterpret_cast it). Luple can be used in constexpr functions if its data members are of literal type. Check the header for API description.

See it in action online at tio.run (also Coliru or Wandbox).

Read the header for API documentation.

nuple: a Named Tuple (C++14)

Header file: nuple.h

nuple extends luple and allows giving names to data members. It works by using C++ string interning (intern.h) which allows for a neat syntax. Check this blog post.

See it in action online at tio.run (also Coliru or Wandbox).

Also a nuple-to-json example at tio.run (also Coliru or Wandbox).

Read the header for API documentation.

C++ String Interning (C++14)

Header file: intern.h

By turning a string literal into a type we can create an interned string in C++. This has a number of useful properties, nuple is one of them. Read more here.

There is a limit on string length of 10 characters (sort of arbitrary), you can increase it by editing the $(...) macro in the intern.h header. Or #define N3599 to use N3599 proposal (adds string literal template to the language) which GCC and Clang implement as an extension, hopefully MSVC will support it too (Update: N3599 enabled by default).

Also you can check an online example here at tio.run (or at Ideone).

Read the header for API documentation.

Struct Reader (C++14)

Header file: struct-reader.h

Struct Reader can detect and create a type list of the data member types of a flat data structure if they are literal, non-const/non-reference types. So, it's a form of primitive RTTI. We can turn the type list into a luple, so that means we can type cast our data to luple. Check the blog post for more details about the technique and online examples. The API description is in the header.

After discovery of the Great Type Loophole there is not much value to Struct Reader, I guess.

Read the header for API documentation.

The C++ Type Loophole (C++14)

Header file: type-loophole.h

Struct Reader is very limited in what it can do. You can't use std::string or a virtual type with it, you need to add types to a list before using them. While researching the thing I uncovered a C++ Type Loophole. Read more in the blog post with online examples.

Read the header for API documentation.


License

The code in this repository is Public-domain software.

Pubic domain software