简体   繁体   English

C++17 部分推导指南

[英]C++17 Partial Deduction Guide

I am trying to write a deduction guide, that only detects one of many typename's from given constructor argument and requires user to enter int size manually我正在尝试编写一个推导指南,该指南仅从给定的构造函数参数中检测到许多类型名中的一个,并要求用户手动输入int size

template <int size, typename T>
struct Board
{
            array<array<T, size>, size> values;

            explicit Board(const vector<T>& raw_values){

            }
};
template <int size, typename T> Board(const vector<T>&) -> Board<int size, T>;

The idea above is that user should still be forced to enter " int size " argument of template, but " typename T " should be deduced from the argument of constructor, is this possible?上面的想法是仍然应该强制用户输入模板的“ int size ”参数,但是应该从构造函数的参数中推导出“ typename T ”,这可能吗?

After correct specification, this is how method should be called正确规范后,这就是方法的调用方式

auto b = Board<3>(initialStateVector);

Currently, it requires to me to enter like this;目前,它需要我这样进入;

auto b = Board<3, int>(initialStateVector);

So basically, I want " int " above to be deduced from given initialStateVector , which has type所以基本上,我希望从给定的initialStateVector推导出上面的“ int ”,它具有类型

const vector<int>& raw_values

The idea above is that user should still be forced to enter "int size" argument of template, but "typename T" should be deduced from the argument of constructor, is this possible?上面的想法是仍然应该强制用户输入模板的“int size”参数,但是应该从构造函数的参数中推导出“typename T”,这可能吗?

According a note (and following examples) in this cppreference page根据此 cppreference 页面中的注释(以及以下示例)

Class template argument deduction is only performed if no template argument list is present.仅当不存在模板参数列表时才执行类模板参数推导。 If a template argument list is specified, deduction does not take place.如果指定了模板参数列表,则不会进行推导。

no, this isn't possible (not in C++17; we can hope in future versions of the standard).不,这是不可能的(不是在 C++17 中;我们希望在标准的未来版本中)。

If you want explicit the size and let deduce the type, the best I can imagine is pass through a good-old make_something function.如果你想要明确的大小并让我们推断类型,我能想象的最好的方法是通过一个古老的 make_something 函数。

I mean something as follows (using std::size_t for the size, as in std::array and almost all STL)我的意思如下(使用std::size_t作为大小,就像在std::array和几乎所有 STL 中一样)

template <std::size_t S, typename T>
Board<S, T> make_Board (std::vector<T> const & v)
 { return {v}; }

// ...

auto b = make_Board<3>(initialStateVector);

that should works also in C++11.这应该也适用于 C++11。

I came up with a workaround using a size hint object我想出了一个使用大小提示对象的解决方法

template<int size>
struct SizeHint {};

Your class would take this as an additional constructor argument:您的类会将其作为额外的构造函数参数:

Board(SizeHint<size>, const std::vector<T>& raw_values)

You call the constructor like this:你像这样调用构造函数:

auto b = Board(SizeHint<2>{}, v);

Bonus奖金

This approach also works for type hints (my original motivation how I found this thread) :这种方法也适用于类型提示(我最初的动机是如何找到这个线程的)

template<typename T>
struct TypeHint{};

template<typename Result, typename T>
struct S {
    S(TypeHint<Result>, T arg) : t{arg}{}
    Result r() {return t;}
    T t;
};

#include <iostream>
int main() {
    S s{TypeHint<int>{}, 5.7};
    std::cout << s.r() << std::endl;
}

This can also be used in combination with variadic templates:这也可以与可变参数模板结合使用:

template<typename Result, typename... Args>
struct S {
    S(TypeHint<Result>, Args... args) : t{args...}{}
    std::tuple<Args...> t;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM