Is it possible to implement always_false in the C++ standard library?

后端 未结 3 1771
野的像风
野的像风 2021-02-07 09:10

There are cases where one uses an always_false helper to e.g. cause unconditional static_assert failure if instantiation of some template is attempted:

相关标签:
3条回答
  • 2021-02-07 09:42

    In C++20, with lambda, you might do something like:

    template <class... T> struct always_false : std::false_type {};
    
    // To have true, but for a type that user code can't reuse as lambda types are unique.
    template <> struct always_false<decltype([](){})> : std::true_type{};
    
    0 讨论(0)
  • 2021-02-07 09:45

    just use option -fdelayed-template-parsing

    0 讨论(0)
  • 2021-02-07 09:54

    To paraphrase Jarod's idea, It could be something like

    template <class... T> struct always_false : std::false_type {};
    
    template <> struct always_false</* implementation defined */> : std::true_type{};
    

    Where /* implementation defined */ can be filled by std::_ReservedIdentifer. User code can't access it, since the identifier is reserved to the library, but there exists a specialization that is true. That should avoid questions about the ODR and lambdas in specializations.

    0 讨论(0)
提交回复
热议问题