// 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