C++17类模板部分推导

C++17 class template partial deduction(C++17类模板部分推导)
本文介绍了C++17类模板部分推导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我对模板的理解类模板的参数推导 提议是在推导上下文中将模板函数和模板类的行为同质化.但我觉得我误解了一些东西.

My understanding about the Template argument deduction for class templates proposal was to homogenize the behaviour of template functions and template classes in deduction contexts. But I think that I have misunderstood something.

如果我们有这个模板对象:

If we have this template object:

template <std::size_t S, typename T>
struct test
{
    static constexpr auto size = S;
    using type_t = T;

    test(type_t (&input)[size]) : data(input) {}
    type_t (&data)[size]{};
};

我倾向于使用辅助函数作为语法糖来创建test对象:

I tend to use a helper function as syntactic sugar for creating test objects:

template <std::size_t S, typename T>
test<S, T> helper(T (&input)[S]) { return input; }

如下图所示:

int main()
{
    int buffer[5];

    auto a = helper<5, int>(buffer); // No deduction
    auto b = helper<5>(buffer);      // Type deduced
    auto c = helper(buffer);         // Type and size deduced

    std::cout << a.size << b.size << c.size;

    return 0;
}

上面的代码按预期输出 555.我使用较新的编译器设置在 Wandbox 中尝试了相同的操作1:

The code above outputs 555 as expected. I've tried the same in Wandbox using the newer compiler setup1:

int main()
{
    int buffer[5];

    test<5, int> a(buffer); // No deduction: Ok.
    test<5> b(buffer);      // Type deduced: FAILS.
    test c(buffer);         // Type and size deduced: Ok.

    std::cout << a.size << b.size << c.size;

    return 0;
}

看起来类模板的模板参数推导仅适用于推导所有参数,我期望两种行为(辅助函数和类模板)是相同的,我是否误解了什么?

It looks like template argument deduction for class templates works only deducing all the parameters, I was expecting both behaviours (helper function and class template) to be the same, did I misunderstood something?

1Wandbox 中最后可用的编译器是 gcc HEAD 7.0.1 201701clang HEAD 5.0.0 (trunk).>

1The last compilers availables in Wandbox are gcc HEAD 7.0.1 201701 and clang HEAD 5.0.0 (trunk).

推荐答案

来自这个优秀的旅行报告 由 Botond Ballo 撰写:

From this excellent trip report by Botond Ballo:

最初提议的功能包括部分推导的规定,您可以在其中明确指定一些模板参数,并将其余部分推导,但由于担心在某些情况下可能会非常混乱,因此将其撤消:

The feature as originally proposed included a provision for partial deduction, where you explicitly specify some of the template arguments, and leave the rest to be deduced, but this was pulled over concerns that it can be very confusing in some cases:

// Would have deduced tuple<int, string, float>,
// but tuple<int> is a well-formed type in and of itself!
tuple<int> t(42, "waldo", 2.0f);

这篇关于C++17类模板部分推导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How do compilers treat variable length arrays(编译器如何处理变长数组)
Deduce template argument from std::function call signature(从 std::function 调用签名推导出模板参数)
check if member exists using enable_if(使用 enable_if 检查成员是否存在)
Standard Library Containers with additional optional template parameters?(具有附加可选模板参数的标准库容器?)
Uses of a C++ Arithmetic Promotion Header(C++ 算术提升标头的使用)
Parameter pack must be at the end of the parameter list... When and why?(参数包必须位于参数列表的末尾...何时以及为什么?)