当函数返回由模板类型和其他类型组成的类型时的模板参数推导 [英] Template argument deduction when the function returns a type composed from the template type and another

查看:123
本文介绍了当函数返回由模板类型和其他类型组成的类型时的模板参数推导的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题很难用文字表达,但这是我试图用不可编译代码实现的目标:

The title is rather hard to formulate in word, but here is what I'm trying to achieve in non-compileable code:

template<template <typename> class Container>
Container<int> foo() {
    return Container<int>{1,2,3};
}

int main() {
    auto bar = foo<std::vector>();
    return 0;
}

基本上,我想要一个模板函数,它可以根据传递给它的类型和先前已知的类型(在本例中为int)组成"其返回类型.在这种情况下,我需要一个函数,该函数在调用方指定的容器内返回任意数据类型. (任意表示,并不是在编译时是随机的或不确定的,而是调用者没有关于数据类型的输入",它是在函数内部确定的).

Basically I want a template function that can "compose" its return type from a type that is passed to it and a previously known type (in this case int). In this case I want a function that returns an arbitrary data type inside a container that is specified by the caller. (By arbitrary I don't mean random or undetermined at compile time, but rather that the caller has no "input" as to what the data type will be, that is determined inside the function itself).

使用clang或带有std + 1z的gcc甚至可以实现这种类型的事情吗?我是否真的缺少明显的东西?是否有一个跨越数百个我不知道的字符的"1行"解决方案?

Is this type of thing even achievable with clang or gcc with std+1z? Am I missing something really obvious? Is there a "1 line" solution spanning hundreds of character that I don't know of?

我在这里看到了许多类似的例子,但它们似乎都假设函数将指针或引用作为参数并填充了这些容器.

I've seen various examples of similar things around here but they all seem to assume the functions take pointers or references as arguments and fill in those containers.

推荐答案

您唯一的麻烦是std::vector不是 template <typename> class. template <typename T, typename Alloc> class恰好有第二个模板参数的默认参数.

Your only trouble here is that std::vector is not a template <typename> class. It is a template <typename T, typename Alloc> class that happens to have a default argument for the second template parameter.

一种解决方法是使用别名模板,该模板实际上仅需要一个参数:

One way around this is using an alias template that really does take just one parameter:

#include <vector>

template<template <typename> class Container>
Container<int> foo() {
    return Container<int>{1,2,3};
}

template <typename T>
using Vec = std::vector<T>;

int main() {
    auto bar = foo<Vec>();
    return 0;
}

在注释中推荐的另一种方法是在声明原始函数的模板时使用可变参数类型名,该函数接受仅接受类型实参的任何类模板.然后,您可以仅使用一个参数实例化它,因为实际模板的第二个参数为默认值.这适用于C ++ 11及更高版本.

Another approach recommended in the comments is to use a variadic typename when declaring the template of the original function, which accepts any class template taking only type arguments. And then you can instantiate it with just one argument, since the actual template has a default for the second. This works in C++11 and above.

#include <vector>

template<template <typename...> class Container>
Container<int> foo() {
    return Container<int>{1,2,3};
}

int main() {
    auto bar = foo<std::vector>();
    return 0;
}

这篇关于当函数返回由模板类型和其他类型组成的类型时的模板参数推导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆