Customization point with user-defined implementation within a designated namespace

Is it possible to provide a customized implementation within a user-defined namespace?
For example, I want a user to implement customization points within a specific namespace, for example namespace custom.

This is an example that works, but not the way I would like to have it:

#include <iostream>

namespace custom
{
    // this does not help. Will result in undefined symbol
    template <typename T>
    void print(const T&);
}

namespace eld
{
    namespace detail
    {
        template<typename T>
        void print(const T &)
        {
            std::cout << "Generic version" << std::endl;
        }

        struct print_fn
        {
            template<typename T>
            void operator()(const T &t) const
            {
                // using custom::print; // this will result in undefined symbol
                print(t);
            }
        };

        template<typename T>
        struct static_constexpr
        {
            static constexpr T value{};
        };

        template<typename T>
        constexpr T static_constexpr<T>::value;
    }

    namespace
    {
        constexpr const auto &print = detail::static_constexpr<detail::print_fn>::value;
    }
}

struct foo
{
};

namespace custom
{
    // this will never be called
    void print(foo) { std::cout << "Custom Implementation for foo" << std::endl; }

    struct bar{};

    void print(bar)
    {
        std::cout << "Custom implementation with ADL for custom::bar" << std::endl;
    }
}

// this will be called
void print(foo) { std::cout << "Implementation for foo" << std::endl; }

int main(int, char **)
{
    eld::print(4);
    eld::print(foo());
    eld::print(custom::bar());

    return 0;
}

The output:

Generic version
Implementation for foo
Custom implementation with ADL for custom::bar

What I want is for custom::print(foo) to be used. It is a preferred way, since some other functions with the same name may be visible in a global namespace. I might use some name prefixes to circumvent this problem, but I’d rather use a designated namespace. Is it possible? (11, 14, 17 Standard)

Source: Windows Questions C++

LEAVE A COMMENT