Deducing template parameters of a template within another template

23 Views Asked by At

I'm trying to deduce the view template arguments in the given expression:

object<bool> object;
object<bool>::view view = object.make_view(std::string("compA"), std::string("compB"));

// instead of expliciting template parameters
// object<bool>::view<false, 2> view = object.make_view(std::string("compA"), std::string("compB"));

With the given code:

template <typename owner_t>
struct object
{
    template <bool const_v, std::size_t component_v>
    struct view
    {
        using object_t = std::conditional_t<const_v, const object, object>;

        object_t& object;
        std::array<std::string, component_v> components;

        template <typename... components_t>
        inline view(object_t& object, components_t&&... components)
            : object(object),
                components {std::forward<components_t>(components)...}
        {
        }
    };

    template <typename... components_t>
    view(const object&, components_t&&...)
        -> view<true, sizeof...(components_t)>;

    template <typename... components_t>
    view(object&, components_t&&...)
        -> view<false, sizeof...(components_t)>;

    template <typename... components_t>
    inline auto make_view(components_t&&... components)
    {
        return view {
            *this,
            std::forward<components_t>(components)...,
        };
    }
};

And it seems to be working for clang, but not for MSVC. If I remove the owner template parameter and make object a normal, non-templated type, it compiles correctly. Has anyone else hit this issue and have you been able to find a solution?

Link to a compiler explorer: https://godbolt.org/z/1Y46rY54T

1

There are 1 best solutions below

0
463035818_is_not_an_ai On

I admit, I don't know why or whether msvc is right the reject your code, but this compiles:

auto vi = obj.make_view(std::string("compA"), std::string("compB"));

Live Demo