Disallow a specific function template instantiation

后端 未结 4 525
醉梦人生
醉梦人生 2021-02-05 23:59

I would like to define a template function but disallow instantiation with a particular type. Note that in general all types are allowed and the generic template works, I just w

4条回答
  •  我寻月下人不归
    2021-02-06 00:29

    I would use a static assert within your function call to create the proper failure during function instantiation:

    template
    class is_double{ static const int value = false; }
    
    template<>
    class is_double{ static const int value = true; }
    
    template
    T convert( const char *argument ){
        BOOST_STATIC_ASSERT( !is_double::value );
        //rest of code
    }
    

    And that should work within a function.

提交回复
热议问题