HTTPS SSH

Build status Build Status

Straight declarations

A header-only C++11 library providing alias templates to declare arrays, pointers and references with uniform syntax and clear semantics. This library makes it possible to write declarations with left-to-right reading order and eliminates reuse of *, &, [ and ] symbols. This library does not aim to eliminate intermediate type aliases or make declarations shorter (though it may happen in certain cases).

// Declare this macro to enable lowercase, std-like syntax.
#define VTT_STRAIGHT_DECLARATIONS_ENABLE_STD_STYLE_SYNTAX
// Include main library header to bring everything into scope.
#include <vtt/Straight Declarations.hpp>

// A C-style array of 3 ints.
int items[3];
vtt::array<3, int> items;

// A pointer to an int.
int * p;
vtt::ptr<int> p;

// A pointer to a function taking nothing and returning an int.
int ( * p_fun )(void);
vtt::ptr<auto (void) -> int> p_fun;

// A pointer to a non-static foo_t class member item
// of const-qualified int type.
const int foo_t::* p_member;
vtt::ptr<foo_t, const int> p_member;

// A pointer to a const-qualified non-static foo_t class
// member function taking nothing and returning an int.
int ( foo_t::* p_member_fun )(void) const;
vtt::ptr<const foo_t, auto (void) -> int> p_member_fun;

// A function taking an rvalue reference to foo_t.
auto action(foo_t && foo) -> int;
auto action(vtt::rvalref<foo_t> foo) -> int;

// A function template taking a universal reference to T.
// Looks the same as if bar was an rvalue reference.
template<typename T> auto action(T && bar) -> int;
// Intentions are clear.
template<typename T> auto action(vtt::uniref<T> bar) -> int;

// We can also mix things together!

using namespace vtt; // for convenience

// A reference to a C-style array of 4 chars.
char ( & items )[4];
ref<array<4, char>> items;

// A pointer to a function taking nothing and returning
// a reference to a C-style array of 4 chars.
char ( & ( * p_fun )(void) )[4];
ptr<auto (void) -> ref<array<4, char>>> p_fun;

Dependencies

Compiler with decent C++11 support. Tested compilers: Visual C++: 14.0, 14.1 g++: 4.8.2, 4.9.4, 5.4.1, 6.3.0 * clang++: 3.5.0, 3.9.1, 4.0.1

Tests also rely on boost 1.65.1 or newer (headers only), headers are assumed to be located in directory "../boost" (relative to this Readme). memcheck.sh script requires valgrind.

Compilation

The library itself is header-only, no compilation is required. Tests can be built using VS solution or using build.sh script. Build artifacts will be stored in "./Build" directory (relative to this Readme).

Usage

In order to use straight declarations in your project directory "./Straight Declarations/" (relative to this Readme) should be added to the list of include directories used by compiler and <vtt/Straight Declarations.hpp> header should be included into your source file. You may also define VTT_STRAIGHT_DECLARATIONS_ENABLE_STD_STYLE_SYNTAX macro before including this header to utilize shorter, std-like syntax.

This library does not depend upon runtime type information, does not define any usable macros.

Reference

namespace n_vtt // this namespace contains declarations with strict names
{

// Alias to a C-style array of VItemsCount items of TItem type.
template<::std::size_t VItemsCount, typename TItem>
t_ArrayOf;

// Alias to a pointer type, depending on template parameters:
// - potentially const-, volatile- qualified type of an item to point to;
// - signature of function to point to;
// - potentially const-, volatile- qualified class type followed by
//   potentially const-, volatile- qualified type of non-static member
//   item to point to;
// - potentially const-, volatile-, lvalue-reference-, rvalue-reference-
//   qualified class type, followed by signature of non-static member
//   function to point to;
template<typename... TParams> using
t_PointerTo;

// Alias to a reference to an item of TItem type.
template<typename TItem> using
t_ReferenceTo;

// Alias to an lvalue reference to an item of TItem type.
// Basically the same as t_ReferenceTo.
template<typename TItem> using
t_LvalueReferenceTo;

// Alias to an rvalue reference to an item of TItem type.
template<typename TItem> using
t_RvalueReferenceTo;

// Alias to a universal reference to an item of TItem type. Since
// universal references only work when used in appropriate template
// instantiation the purpose of this declaration is to increase clearity.
template<typename TItem> using
t_UniversalReferenceTo;

} // namespace n_vtt

namespace vtt // this namespace contains declarations with std-like names
{

// The same as ::n_vtt::t_ArrayOf. Notice that the order of template
// parameters is reversed compared with ::std::array, so there will be
// no clash if both std and vtt namespaces are in use.
template<::std::size_t VItemsCount, typename TItem>
array;

// The same as ::n_vtt::t_PointerTo.
template<typename... TParams> using
ptr;

// The same as ::n_vtt::t_ReferenceTo.
template<typename TItem> using
ref;

// The same as ::n_vtt::t_LvalueReferenceTo.
template<typename TItem> using
lvalref;

// The same as ::n_vtt::t_RvalueReferenceTo.
template<typename TItem> using
rvalref;

// The same as ::n_vtt::t_UniversalReferenceTo.
template<typename TItem> using
uniref;

} // namespace vtt

Author