Skip to content

badair/sfinae_errors

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

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.

Releases

No releases published

Packages

No packages published

Languages