badair / sfinae_errors

SFINAE-able error messages for C++ templates

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SfinaeErrors Try it online

A single C++ header file for SFINAE-able error messages. Templated code with SFINAE-able error messages is easier to debug, and doesn't suffer from the inflexibility that comes with static_assert.

    
    #include <type_traits>
    #include "sfinae_errors.hpp"
    
    // An error "origin" describes the source of the substitution failure
    DEFINE_SFINAE_ERROR_ORIGIN(const_copyable)
    
    // T_must_be_const and T_must_be_copy_constructible are 
    // SFINAE "exceptions", associated with const_copyable
    DEFINE_SFINAE_ERROR_FOR(const_copyable, T_must_be_const)
    DEFINE_SFINAE_ERROR_FOR(const_copyable, T_must_be_copy_constructible)
        
    // This is an alias template that aliases T, but SFINAEs
    // when T is not const or when T is not copy constructible.
    // Note that if the aliased type is a template, it is not
    // instantiated until after the checks are performed.
    template<typename T>
    using const_copyable = sfinae_errors::sfinae_try< T,
        sfinae_errors::fail_if<!std::is_copy_constructible<T>::value, T_must_be_copy_constructible>,
        sfinae_errors::fail_if<!std::is_const<T>::value, T_must_be_const>
    >;
    
    int main(){
        
        // compiler error:
        //     no type named 'type' in 'error::const_copyable<T_must_be_const_>::_'
        
        const_copyable<int> i = 0;
        
        return i;
    }
    

Compatibility

  • Clang 3.4 and later
  • GCC 4.7.3 and later
  • Visual Studio 2015

Older platforms are untested.

Distributed under the Boost software license.

About

SFINAE-able error messages for C++ templates

License:Boost Software License 1.0


Languages

Language:C++ 100.0%