Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Another use of std::monostate is as a special "unset/don't care" value for a template parameter. eg

    template<typename T = std::monostate>
    class C
    {
        ...

        if constexpr (!std::is_same_v<T, std::monostate>
        {
            // T-related behaviour here
        }
    };


I greatly prefer tag classes for this. You can define 'em in a single line and a downstream user can't accidentally plug the unset value in to the template.


You can use void for that.


void could be an actual meaningful type for a template rather than a dummy type.


You could say the same thing about std::monostate, which is not a dummy type. If you need a unique sentinel type you have to make one for that purpose.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: