2017-10-01

Straight Declarations C++ library

Historically C++ declaration syntax has been a total mess. Spiral rule and operators reused to declare stuff can make declarations unreadable to the point that people create special demangling services. With the recent introduction of rvalue- and universal- references situation became even worse.

C++ declarations reading meme.

Straight Declarations is 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).

Appveyor CI build status. Travis CI build status.

Downloads for rev. 68

Mercurial (Bitbucket mirror) clone:
hg clone https://bitbucket.org/guaranteed-to-be-unique/straight-declarations
Git (Github mirror) clone:
git clone https://github.com/guaranteed-to-be-unique/straight-declarations.git

Examples

// 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. Verification script memcheck.sh requires valgrind.

Known issues

  • Building projects using VS emits warning "MSB8029: The Intermediate directory or Output directory cannot reside under the Temporary directory as it could lead to issues with incremental build.". This warning is caused by use of $(TEMP) as Intermediate directory. None of the projects use incremental building so this warning can be safely ignored.
  • Building Test project emits quite a lot of warnings from the depths of Boost.Test implementation. Though most of them are rather harmless boost developers really need to get their shit together.
  • VS successfully compiles test Array_Of_ZeroItemsCompilationFailure.
  • VS emits errors (including ICE) when instantiating a template among parameters of which there is a function signature with another template instantiated as a trailing return type. For example:
    ptr
    <auto (
    void
    ) ->
    ref
    <
    int
    >>
    p_fun
    ;
    Heading return type can be used as a workaround.