mardi 31 octobre 2017

Partially filled template as parameter for template template

I have a template with two template arguments (MyCollection) and another template (TCTools) expecting a template with one template argument as template argument.

I defined a "bridge" (TypedCollection) to get a template with one parameter from MyCollection and an argument for it's first parameter with the aim of passing it to the template template.

This works fine, if I use the bridge with a fixed type as argument, but calling it from yet another template with the argument of that other template will not compile.

#include <iostream>
using std::size_t;

template <class Scalar, size_t size>
struct MyCollection
{
    MyCollection()
    {
        std::cout << "Made collection"
                  << std::endl
                  << "  " << __PRETTY_FUNCTION__
                  << std::endl;
    }
};

template <class Scalar>
struct TypedCollection
{
    template <size_t size>
    using value = MyCollection<Scalar, size>;
};

template <template <size_t> class TC>
struct TCTools
{
    static TC<10> *make_10_sized()
    {
        return new TC<10>();
    }
};

template <class S>
void test()
{
    // Will not compile
    TCTools<TypedCollection<S>::value>::make_10_sized();
}

int main()
{
    // works
    TCTools<TypedCollection<int>::value>::make_10_sized();

    test<int>();
    return 0;
}

GCC gives the following note:

expected a class template, got ‘TypedCollection<S>::value’

The whole thing has left me very confused. Why is the call in test() not compiling, while the one in main() works just as expected? Is it possible to get test to work?

Aucun commentaire:

Enregistrer un commentaire