diff --git a/doc/type_index.qbk b/doc/type_index.qbk index f7946e1..27d8777 100644 --- a/doc/type_index.qbk +++ b/doc/type_index.qbk @@ -268,6 +268,12 @@ Issues with cross module type comparison on a bugged compilers are bypassed by d [import ../examples/table_of_names.cpp] [section Table of raw_name() and pretty_name() outputs with and without RTTI ] [type_index_names_table] [endsect] +[import ../examples/constexpr14_namespace_check.cpp] +[section C++14: Checking namespace at compile time ] [type_index_constexpr14_namespace_example] [endsect] + +[import ../examples/constexpr14_sort_check.cpp] +[section C++14: Checking lexigraphical order of provided types ] [type_index_constexpr14_sort_check_example] [endsect] + [endsect] [xinclude autodoc.xml] diff --git a/examples/constexpr14_namespace_check.cpp b/examples/constexpr14_namespace_check.cpp index 13141e1..0752e08 100644 --- a/examples/constexpr14_namespace_check.cpp +++ b/examples/constexpr14_namespace_check.cpp @@ -17,7 +17,7 @@ constexpr bool starts_with(const char* name, const char (&ns)[N]) noexcept { return true; } -//[type_index_constexpr14_example +//[type_index_constexpr14_namespace_example /*` The following example shows that `boost::typeindex::ctti_type_index` is usable at compile time on a C++14 compatible compilers. @@ -82,7 +82,7 @@ int main() { // short sh = 0; // s.serialize(sh); // Fails the static_assert! } -//] [/type_index_constexpr14_example] +//] [/type_index_constexpr14_namespace_example] #else // #if !defined(BOOST_NO_CXX14_CONSTEXPR) && !defined(BOOST_NO_CXX11_CONSTEXPR) diff --git a/examples/constexpr14_sort_check.cpp b/examples/constexpr14_sort_check.cpp new file mode 100644 index 0000000..5443924 --- /dev/null +++ b/examples/constexpr14_sort_check.cpp @@ -0,0 +1,76 @@ +// Copyright 2013-2016 Antony Polukhin + +// Distributed under the Boost Software License, Version 1.0. +// (See the accompanying file LICENSE_1_0.txt +// or a copy at .) + +#include + +#if !defined(BOOST_NO_CXX14_CONSTEXPR) && !defined(BOOST_NO_CXX11_CONSTEXPR) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + +//[type_index_constexpr14_sort_check_example +/*` + The following example shows that `boost::typeindex::ctti_type_index` is usable at compile time on + a C++14 compatible compilers to check order of template parameters. + + Consider the situation when we have a function that accepts std::tuple, boost::variant or some other class that uses variadic templates: +*/ + +template class types{}; + +template +void do_something(const types& t) noexcept; + +/*` + Such functions may be very usefull, however they may significantly increase the size of the resulting program. Each instantionation of such function with different templates order + consumes space in the resulting program: + + // Types are same, but different order leads to new instantionation of do_something function. + types + types + types + types + types + types + + One of the ways to reduce instantinations count is to force the types to have some order: +*/ + + +#include + +// Implementing type trait that returns true if the types are sorted lexographicaly +template +constexpr bool is_asc_sorted(types) noexcept { + return true; +} + +template +constexpr bool is_asc_sorted(types) noexcept { + using namespace boost::typeindex; + return ctti_type_index::type_id() <= ctti_type_index::type_id() + && is_asc_sorted(types()); +} + + +// Using the newly created `is_asc_sorted` trait: +template +void do_something(const types& t) noexcept { + static_assert( + is_asc_sorted( types() ), + "T... for do_something(const types& t) must be sorted ascending" + ); +} + +int main() { + do_something( types() ); + // do_something( types() ); // Fails the static_assert! +} +//] [/type_index_constexpr14_sort_check_example] + +#else // #if !defined(BOOST_NO_CXX14_CONSTEXPR) && !defined(BOOST_NO_CXX11_CONSTEXPR) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + +int main() {} + +#endif +