2
\$\begingroup\$

I've created the following program to assist me in some template stuff I'm working on.

The function get_type returns 0, 1, 2, 3 or nullopt corresponding to which group the argument T belongs to.

This is my first template style project, so I'm looking for any critique possible. (Note the functions are marked static intentionally, as this will only be used inside this particular file, and should not be visible to users.)

namespace ae
{
    namespace details
    {
        template <class T>
        [[nodiscard]]
        static inline constexpr auto is_integer() noexcept
        {
            return std::is_same_v<T, short>
                || std::is_same_v<T, int>
                || std::is_same_v<T, long>
                || std::is_same_v<T, long long>
                || std::is_same_v<T, unsigned short>
                || std::is_same_v<T, unsigned int>
                || std::is_same_v<T, unsigned long>
                || std::is_same_v<T, unsigned long long>;
        }

        template <class T>
        [[nodiscard]]
        static inline constexpr auto is_floating_point() noexcept
        {
            return std::is_same_v<T, float>
                || std::is_same_v<T, double>
                || std::is_same_v<T, long double>;
        }

        template <class T>
        [[nodiscard]]
        static inline constexpr auto is_charachter() noexcept
        {
            return std::is_same_v<T, char>
                || std::is_same_v<T, unsigned char>
                || std::is_same_v<T, wchar_t>;
        }

        template <class T>
        [[nodiscard]]
        static inline constexpr auto is_boolean() noexcept
        {
            return std::is_same_v<T, bool>;
        }

        // Returns 0 for integer, 1 for floating_point,
        // 2 for charachter, 3 for boolean, and nullopt otherwise
        template <class T>
        [[nodiscard]]
        static inline constexpr auto get_type() noexcept
        {
            if constexpr (is_integer<T>())        
                return std::optional<unsigned int>(0u);
            else if constexpr (is_floating_point<T>()) 
                return std::optional<unsigned int>(1u);
            else if constexpr (is_charachter<T>())     
                return std::optional<unsigned int>(2u);
            else if constexpr (is_boolean<T>())       
                return std::optional<unsigned int>(3u);
            else return std::nullopt;
        }
    }
}

Example usage

template <typename T>
static typename std::enable_if<ae::details::is_integer<T>(), T>::type test()
{
    // Do something if T is integer
}

template <typename T>
static typename std::enable_if<ae::details::is_charachter<T>(), T>::type test()
{
    // Do something if T is charachter
}

int main()
{
    test<int>(); //..

    return 0;
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Add a real test / use case please. \$\endgroup\$ Commented Mar 26, 2020 at 18:51
  • 1
    \$\begingroup\$ Added a quick example usage. \$\endgroup\$
    – Cortex
    Commented Mar 26, 2020 at 19:11
  • 1
    \$\begingroup\$ The Example usage from your first addition leaves me none the wiser. \$\endgroup\$
    – greybeard
    Commented Mar 27, 2020 at 6:48

1 Answer 1

1
\$\begingroup\$

In is_charachter (which is misspelled; it should be is_character), you left out signed char (which is a distinct type from char). The same applies for all of the fixed width types (char8_t, char16_t, char32_t).

get_type returns some arbitrary magic number values. You could create named constants to represent the type. If you add a "unknown_type" value, then you wouldn't need to use std::optional.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.