Defining a templated friend function inside a template class

Viewed 339

Why does the following code yield the compiler error no template named make_static_vector?

template<class Tuple>
class vector;

template<typename T, std::size_t N>
using static_vector = vector<std::array<T, N>>;

template<class Tuple>
class vector
{
private:
    using value_type = std::decay_t<decltype(std::declval<Tuple&>().operator[](0))>;

    template<typename T, typename... Elements>
    friend static_vector<T, sizeof...(Elements)> make_static_vector(Elements&&... elements) {
        return { std::forward<Elements>(elements)... };
    }

    template<typename... Elements>
    vector(Elements&&... elements)
        : m_elements{ static_cast<value_type>(std::forward<Elements>(elements))... }
    { }

    Tuple m_elements;
};

int main()
{
    make_static_vector<double>(1, 1);   
    return 0;
}

I've created a live demo of the code. It's working when I move the definition of make_static_vector outside the class and leave only the declaration part inside the class.

Why is it not possible to define the function directly inside the class?

1 Answers
Related