包含类型的类型的模板函数重载 [英] Template function overload for type containing a type

查看:101
本文介绍了包含类型的类型的模板函数重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作:

I'm trying to do the following:

#include <iostream>
#include <vector>
#include <tuple>
#include <list>

template <typename T>
void f(T t) {
    std::cout << "1" << std::endl;
}

template <typename T, typename V>
void f(T<std::tuple<V>> t) {
    std::cout << "2" << std::endl;
}

int main() {
    f(std::list<double>{}); // should use first template
    f(std::vector<std::tuple<int>>{}); // should use second template
}

在C ++ 14中最简单的方法是什么?我以为我可以通过这种方式进行模式匹配,但是编译器将没有这种模式.

What is the simplest way to do this in C++14? I thought that I could sort of pattern match in this way but the compiler won't have it.

推荐答案

模板参数T用作模板名称,因此应将其声明为

The template parameter T is used as a template-name, so it should be declared as template template parameter. e.g.

template <template <typename...> class T, typename V>
//        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
void f(T<std::tuple<V>> t) {
    std::cout << "2" << std::endl;
}

实时

这篇关于包含类型的类型的模板函数重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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