hana::make_map 使用提升模板

hana::make_map with lifted template

我正在尝试在编译时创建键任务对映射。键是一个序列号,它也应该用作映射任务类型内部的模板参数。我学到的是,我需要将我的任务提升到一个元函数中才能完成这项工作,但我在创建与我的模板参数匹配的正确 hana::types 时遇到了问题。

这是我目前拥有的:

template <std::size_t Key,
          typename T = double,
          template<typename...> class Complex = std::complex>
class Task
{
    ...
}

template <std::size_t Begin,
          std::size_t End,
          typename T,
          template<typename...> class Complex = std::complex>
class TaskFactory
{
    static constexpr auto create(void)
    {
        auto keys = hana::make_range(hana::int_c<Begin>, hana::int_c<End>);

        return hana::unpack(keys, [](auto... key)
        {
            return hana::make_map(hana::make_pair(key, hana::template_<Task>(hana::type_c<key>, hana::type_c<T>, hana::type_c<Complex>)())...);
        });
    }

    static constexpr auto taskMap_ = create();

    ...
}

int main()
{
    TaskFactory<2, 8, double, std::complex> myTaskFactory;
    return 0;
}

Clang 正在抱怨:

error: template template argument has different template parameters than its corresponding template template parameter

我做错了什么,这是正确的方法吗?

最佳 嗯

hana::template_ 仅适用于 typename 模板参数 - 它不支持 非类型模板参数 模板模板参数。参见 its implementation here

hana::type_c也是如此。

  • hana::type_c<key> 无效,因为 key 不是类型。

  • hana::type_c<Complex> 无效,因为 Complex 不是类型。

你对 hana::unpack(keys, [](auto... key) 的处理方式对我来说还不错。您需要将 Task class 更改为根据类型定义 - 例如:

template <typename Key,
          typename Complex>
class Task
{
    // ...
};

这样,您就可以按照自己的意愿使用 hana::template_